關於commons-fileupload組件上傳文件中文名亂碼問題

  java web開發,經常使用到的文件上傳功能,經常使用的commons-fileupload和commons-io兩個jar包。關於如何使用這兩個jar來完成文件上傳的功能,這裏不作詳解。使用commons-fileupload來上傳文件,有兩種方式能夠是實現。html

1. 傳統API前端

 1 <%@page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" language="java"%>
 2 <%@page import="java.io.File"%>
 3 <%@page import="java.util.List"%>
 4 <%@page import="org.apache.commons.fileupload.*"%>
 5 <%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
 6 <%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
 7 
 8 <%
 9     request.setCharacterEncoding("UTF-8");
10     // file less than 10kb will be store in memory, otherwise in file system.
11     final int threshold = 10240;
12     final File tmpDir = new File(getServletContext().getRealPath("/") + "fileupload" + File.separator + "tmp");
13     final int maxRequestSize = 1024 * 1024 * 4;    // 4MB
14 
15     // Check that we have a file upload request
16     if(ServletFileUpload.isMultipartContent(request))
17     {
18         // Create a factory for disk-based file items.
19         FileItemFactory factory = new DiskFileItemFactory(threshold, tmpDir);
20         
21         // Create a new file upload handler
22         ServletFileUpload upload = new ServletFileUpload(factory);
23 
24         // Set overall request size constraint.
25         upload.setSizeMax(maxRequestSize);
26 
27         List<FileItem> items = upload.parseRequest(request);    // FileUploadException 
28         for(FileItem item : items)
29         {
30             if(item.isFormField())    //regular form field
31             {
32                 String name = item.getFieldName();
33                 String value = item.getString();
34 %>
35                 <h1><%=name%> --> <%=value%></h1>
36 <%
37             }
38             else
39             {    //file upload
40                 String fieldName = item.getFieldName();
41                 String fileName = item.getName();
42                 File uploadedFile = new File(getServletContext().getRealPath("/") +
43                             "fileupload" + File.separator + fieldName + "_" + fileName);
44                 item.write(uploadedFile);
45 %>
46                 <h1>upload file <%=uploadedFile.getName()%> done!</h1>
47 <%
48             }
49         }
50     }
51 %>

2. Stream APIjava

 1 <%@page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" language="java"%>
 2 <%@page import="java.io.*"%>
 3 <%@page import="java.util.List"%>
 4 <%@page import="org.apache.commons.fileupload.*"%>
 5 <%@page import="org.apache.commons.fileupload.util.Streams"%>
 6 <%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
 7 
 8 <%
 9     request.setCharacterEncoding("UTF-8");
10 
11     // Check that we have a file upload request
12     if(ServletFileUpload.isMultipartContent(request))
13     {    
14         // Create a new file upload handler
15         ServletFileUpload upload = new ServletFileUpload();
16         
17         // Parse the request
18         FileItemIterator iter = upload.getItemIterator(request);
19 
20         while(iter.hasNext())
21         {
22             FileItemStream item = iter.next();
23             String fieldName = item.getFieldName();
24             InputStream is = item.openStream();
25 
26             if(item.isFormField())    //regular form field
27             {
28 %>
29                 <!-- read a FileItemStream's content into a string. -->
30                 <h1><%=fieldName%> --> <%=Streams.asString(is)%></h1>
31 <%
32             }
33             else
34             {    //file upload
35                 String fileName = item.getName();
36                 File uploadedFile = new File(getServletContext().getRealPath("/") +
37                             "fileupload" + File.separator + fieldName + "_" + fileName);
38                 OutputStream os = new FileOutputStream(uploadedFile);
39                 // write file to disk and close outputstream.
40                 Streams.copy(is, os, true);
41 %>
42                 <h1>upload file <%=uploadedFile.getName()%> done!</h1>
43 <%
44             }
45         }
46     }
47 %>

  Streaming API上傳速度相對較快。由於它是利用內存保存上傳的文件,節省了傳統API將文件寫入臨時文件帶來的開銷。天然,更傾向於選用Stream API。web

  前端實現文件上傳的「手段」,目前接觸過的有UEditor的上傳組件、jQuery的ajaxfileupload以及AngularJs的集成上傳組件。發送的也都是正常的上傳表單數據,而當使用Stream API,獲取文件名時,「String fileName = item.getName()」,當文件名爲中文名時,會出現亂碼現象。
ajax

  網上搜尋的關於設置request編碼的法子都無論用,嘗試了屢次後,找到了解決辦法,重點在於上面代碼中的ServletFileUpload upload,添加upload.setHeaderEncoding("UTF-8");便可解決中文文件名的亂碼問題了。apache

相關文章
相關標籤/搜索