fileupload上傳中文文件名時出現亂碼問題


昨天讓這個亂碼問題弄了很久,一大早就開始想要怎么解決才好。
很簡單上傳頁面,jsp上傳頁面代碼

1    <form action="/struts2/UploadServlet" method="post" enctype="multipart/form-data"> 
2        用戶名:<input type="text" name="username"><br>
3        密 碼:<input type="password" name="password"><br>
4        文件1:<input type="file" name="file1"><br>
5        文件2:<input type="file" name="file2"><br>
6        <input type="submit" value="提交">
7    </form> 

下面是UploadServlet代碼

 1@SuppressWarnings("serial")
 2public class UploadServlet extends HttpServlet {
 3
 4    @SuppressWarnings("unchecked""deprecation" })
 5    public void doPost(HttpServletRequest request, HttpServletResponse response)
 6            throws ServletException, IOException {
 7//            設置工廠
 8            DiskFileItemFactory factory = new DiskFileItemFactory();
 9            String path = request.getRealPath("/upload");
10//            設置文件存儲位置
11            factory.setRepository(new File(path));
12//            設置大小,如果文件小於設置大小的話,放入內存中,如果大於的話則放入磁盤中
13            factory.setSizeThreshold(1024*1024);
14            
15            ServletFileUpload upload = new ServletFileUpload(factory);
16//            這里就是中文文件名處理的代碼,其實只有一行,serheaderencoding就可以了
17            upload.setHeaderEncoding("utf-8");
18            /*String enCoding = request.getCharacterEncoding();
19            if(enCoding != null){
20                upload.setHeaderEncoding(enCoding);
21            }*/
22            
23            try {
24                List<FileItem> list = upload.parseRequest(request);
25                for(FileItem item : list){
26//                    判斷是不是上傳的文件,如果不是得到值,並設置到request域中
27//                    這里的item.getfieldname是得到上傳頁面上的input上的name
28                    if(item.isFormField()){
29                        String name = item.getFieldName();
30                        String value =item.getString("utf-8");
31                        System.out.println(name);
32                        System.out.println(value);
33                        request.setAttribute(name, value);
34                    }
35//                    如果是上傳的文件,則取出文件名,
36                    else{
37                        String name = item.getFieldName();
38                        String value = item.getName();
39                        System.out.println(name);
40                        System.out.println(value);
41//                        得到不要地址的文件名,不同的瀏覽器傳遞的參數不同,有的直接傳遞文件名,而又的把文件地址一起傳遞過來
42//                        使用substring方法可以統一得到文件名而不得到文件位置
43                        int start = value.lastIndexOf("\\");
44                        String fileName = value.substring(start + 1);
45                        request.setAttribute(name, fileName);
46//                        寫文件到path目錄,文件名問filename
47                        item.write(new File(path,fileName));
48                    }
49                }
50            }
51        
52            catch (FileUploadException e) {
53                e.printStackTrace();
54            } catch (Exception e) {
55                e.printStackTrace();
56            }
57//           跳轉到顯示結果頁面
58            request.getRequestDispatcher("upload/result2.jsp").forward(request, response);
59    }
60
61}

用EL表達式顯示輸出

1  <body>
2     用戶名:${requestScope.username } <br>
3     密  碼:${requestScope.password } <br>
4     文件1 :${requestScope.file1 }<br>
5     文件2 :${requestScope.file2 }<br>
6  </body>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM