当前位置: 编程技术>java/j2ee
jsp文件上传与下载实例代码
来源: 互联网 发布时间:2014-10-19
本文导语: 文件上传: 代码如下: public class UploadServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse re...
文件上传:
public class UploadServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
SmartUpload myupload = new SmartUpload();
ServletConfig config = getServletConfig();
myupload.initialize(config,req,resp);
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
out.println("处理上传的文件");
out.println("");
try {
myupload.setMaxFileSize(1024*1024);
myupload.setTotalMaxFileSize(5*1024*1024);
myupload.setAllowedFilesList("doc,txt,jpg,gif");
myupload.setDeniedFilesList("exe,bat,jsp,htm,html");
myupload.upload();//上传文件,此项是必须的
int count = myupload.getFiles().getCount();
Request myRequest = myupload.getRequest();
String rndFilename,fileExtName,fileName,filePathName;
Date dt = null;
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
//一一提取上传文件信息,同时可保持文件
for(int i = 0; i < count; i++){
File file = myupload.getFiles().getFile(i);
if(file.isMissing())
continue;
fileName = new String(file.getFileName().getBytes(),"utf-8");//获取文件名
filePathName = new String(file.getFilePathName().getBytes(),"utf-8");
fileExtName = file.getFileExt();
dt = new Date(System.currentTimeMillis());
Thread.sleep(100);
rndFilename = fmt.format(dt)+i+"."+fileExtName;
out.println("第"+(i+1)+"个文件的文件信息:
");
out.println(" 文件名为:"+fileName+"
");
out.println(" 文件扩展名为:"+fileExtName+"
");
out.println(" 文件全名:"+filePathName+"
");
out.println(" 文件大小:"+file.getSize()/1024+"kb
");
//创建文件保存位置
ServletContext context = req.getServletContext();
String savePath = context.getRealPath("/upload/");
java.io.File folder = new java.io.File(savePath);
if(!folder.exists()){
folder.mkdirs();
}
out.println(" 文件保存路径:"+savePath);
file.saveAs(savePath+"/"+rndFilename,SmartUpload.SAVE_PHYSICAL);
}
} catch (Exception e) {
out.println("文件上传失败!!!!");
e.printStackTrace();
}
out.flush();
out.close();
}
}
上传文件实例
$(function(){
$("#number").change(function(){
var number = $("#number").attr("value");
$("#files").html("");
for(var i = 0; i < number; i++){
$("#files").append("
");
}
});
});
上传文件实例
请选择上传文件数量:
1
2
3
4
5
文件下载:
代码如下:
public class UploadServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
SmartUpload myupload = new SmartUpload();
ServletConfig config = getServletConfig();
myupload.initialize(config,req,resp);
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
out.println("处理上传的文件");
out.println("");
try {
myupload.setMaxFileSize(1024*1024);
myupload.setTotalMaxFileSize(5*1024*1024);
myupload.setAllowedFilesList("doc,txt,jpg,gif");
myupload.setDeniedFilesList("exe,bat,jsp,htm,html");
myupload.upload();//上传文件,此项是必须的
int count = myupload.getFiles().getCount();
Request myRequest = myupload.getRequest();
String rndFilename,fileExtName,fileName,filePathName;
Date dt = null;
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
//一一提取上传文件信息,同时可保持文件
for(int i = 0; i < count; i++){
File file = myupload.getFiles().getFile(i);
if(file.isMissing())
continue;
fileName = new String(file.getFileName().getBytes(),"utf-8");//获取文件名
filePathName = new String(file.getFilePathName().getBytes(),"utf-8");
fileExtName = file.getFileExt();
dt = new Date(System.currentTimeMillis());
Thread.sleep(100);
rndFilename = fmt.format(dt)+i+"."+fileExtName;
out.println("第"+(i+1)+"个文件的文件信息:
");
out.println(" 文件名为:"+fileName+"
");
out.println(" 文件扩展名为:"+fileExtName+"
");
out.println(" 文件全名:"+filePathName+"
");
out.println(" 文件大小:"+file.getSize()/1024+"kb
");
//创建文件保存位置
ServletContext context = req.getServletContext();
String savePath = context.getRealPath("/upload/");
java.io.File folder = new java.io.File(savePath);
if(!folder.exists()){
folder.mkdirs();
}
out.println(" 文件保存路径:"+savePath);
file.saveAs(savePath+"/"+rndFilename,SmartUpload.SAVE_PHYSICAL);
}
} catch (Exception e) {
out.println("文件上传失败!!!!");
e.printStackTrace();
}
out.flush();
out.close();
}
}
代码如下:
上传文件实例
$(function(){
$("#number").change(function(){
var number = $("#number").attr("value");
$("#files").html("");
for(var i = 0; i < number; i++){
$("#files").append("
");
}
});
});
上传文件实例
请选择上传文件数量:
1
2
3
4
5
文件下载:
代码如下: