req.setCharacterEncoding("utf-8");
Set<String> extSet = new HashSet<String>();
extSet.add("xls");
final long MAX_SIZE = 2*1024*1024;
DiskFileItemFactory dfif = new DiskFileItemFactory();
//设置临时文件的存储位置,文件夹
dfif.setRepository(new File(req.getRealPath("/")+"catch\\"));
ServletFileUpload sfu = new ServletFileUpload(dfif);
//限制上传文件大小
sfu.setSizeMax(MAX_SIZE);
List<FileItem> fileList = null;
//,解析请求正文,获取上传文件
try{
fileList = sfu.parseRequest(req);
}catch(FileUploadException e){
System.out.println("解析出错");
e.printStackTrace();
}
//对一个文件实体进行处理
FileItem fileItem = fileList.get(0);
String path = fileItem.getName();
String t_ext = path.substring(path.lastIndexOf(".")+1);
if(!extSet.contains(t_ext)){
req.setAttribute("info","只允许传.xls格式的文件");
req.getRequestDispatcher("index.jsp").forward(req, resp);
return;
}
long now = System.currentTimeMillis();
String prefix = String.valueOf(now);
//存储在本地的真实文件名
String u_name = req.getRealPath("/")+"file\\"+prefix+"."+t_ext;
//文件实体写入本地
try{
fileItem.write(new File(u_name));
}catch(Exception e){
e.printStackTrace();
}
- 浏览: 24602 次
相关推荐
java中用common-fileupload上传文件的一个很简单的demo展示
Common-FileUpload带进度条文件上传
Struts2拦截器学习.doc Common-FileUpload实现文件上传.doc 写得很好
NULL 博文链接:https://royzhou1985.iteye.com/blog/336866
Common-fileupload组件实现文件上传.pdfCommon-fileupload组件实现文件上传.pdfCommon-fileupload组件实现文件上传.pdfCommon-fileupload组件实现文件上传.pdfCommon-fileupload组件实现文件上传.pdfCommon-...
java+servlet+commons-io-2.4.jar+commons-fileupload-1.3.jar实现文件的上传与下载
NULL 博文链接:https://arron-huang.iteye.com/blog/1032585
个人使用common-fileupload1.2.2和io-1.3.2时总结
commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar jar 文件。 commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar 案例上传: http://hi.baidu.com/lichao77821/blog commons-fileupload-1.2.1.jar和commons-...
浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用Servlet获取上传文件的输入流然后再解析里面的请求参数是比较麻烦,所以一般选择采用apache的开源工具common-fileupload这个文件上传组件。
NULL 博文链接:https://412887952-qq-com.iteye.com/blog/1366123
利用commons-fileupload组件实现的java的文件的上传与下载。
最近进行了整理,总结了一下,供大家参考。
使用apache的commons-fileupload上传文件
用开源的FileUpload组件,可以很方便的给自己的系统添加功能强大的文件上传模块。 该资源文件是两个jar打包成的压缩文件。
commons-fileupload-1.2.2.jar和commons-io-2.4.jar包,文件上传和下载功能必须要的两个jar文件
讲述如何使用commons-fileupload进行文件的上传以及下载!内容很详细!
commons-fileupload-1.3.1-bin commons-io-2.4-bin java上传使用的架包和它的文档以及源代码
fileList = upload.parseRequest(request);上传file文件和form表单提交 common-fileupload实例
源码 博文链接:https://lisanlai.iteye.com/blog/660238