Java 上传文件总结

1. 使用commons-fileupload.jar+commons-io.jar 上传

代码
 1 try {
 2             DiskFileItemFactory factory = new DiskFileItemFactory();
 3             factory.setSizeThreshold(1024 * 1024 * 10);
 4             factory.setRepository(new File(request.getRealPath("/")));
 5             System.out.println(">>>" + request.getParameter("file5"));
 6             ServletFileUpload upload = new ServletFileUpload(factory);
 7             upload.setSizeMax(1024 * 1024 * 10);
 8             List<FileItem> items = upload.parseRequest(request);
 9             for (FileItem item : items) {
10                 if (item.isFormField()) {
11                     String name = item.getFieldName();
12                     String value = item.getString("GBK");
13                     System.out.println(name + "-----" + value);
14                 } else {
15                     String fieldName = item.getFieldName();
16                     String fileName = item.getName();
17                     System.out.println("文件域名称:" + fieldName);
18                     System.out.println("文件名:" + fileName);
19                     System.out.println("文件类型:" + item.getContentType());
20                     FileOutputStream fos = new FileOutputStream(request
21                             .getRealPath("/")
22                             + fileName.substring(fileName.lastIndexOf("\\")));
23                     if(item.isInMemory()){
24                         fos.write(item.get());
25                     }else{
26                         InputStream is=item.getInputStream();
27                         byte[] buffer=new byte[1024];
28                         int len=0;
29                         while((len=is.read())>0){
30                             fos.write(buffer,0,len);
31                         }
32                         is.close();
33                         fos.close();
34                     }
35                 }
36             }
37         } catch (Exception e) {
38             System.out.println(e.getMessage());
39         }

   点击此处下载核心文件包 /Files/qingyuan/lib.rar

  getFieldName()获取表单域的name属性值

  getString(String encoding) 获得表单域的Value属性值,参数为编码集

  getName()获得上传文件的名称

  getContentType()获得上传文件的类型

  get()获得上传文件的字节内容

(2) 使用COS上传

代码
 1 MultipartParser mp=new MultipartParser(request,10*1024*1024);
 2         Part part=null;
 3         while((part=mp.readNextPart())!=null){
 4             String name=part.getName();
 5             System.out.println("表单域的名称:"+name);
 6             if(part.isParam()){
 7                 ParamPart paramPart=(ParamPart)part;
 8                 String value=paramPart.getStringValue("GBK");
 9                 System.out.println("值:"+value);
10             }else if(part.isFile()){
11                 FilePart filePart=(FilePart)part;
12                 String fileName=filePart.getFileName();
13                 if(fileName!=null){
14                     filePart.writeTo(new File(request.getRealPath("/")+fileName));
15                 }
16             }
17         }

  使用COS上传文件需要使用cos.jar这个包,可以到Http://www.servlets.com/cos 这个网站下载

  使用COS上传文件比Commons-fileupload 要方便许多,但是对中文支持不好,如果上传文件中有中文会出现乱码。所以建议使用Commons-fileupload.jar 上传。其实Struts2 中的 文件上传也是依赖这个包,只不过是进行了更进一步的封装。

原文地址:https://www.cnblogs.com/qingyuan/p/1621331.html