fileupload上傳中文文件名時(shí)亂碼問題

          昨天讓這個(gè)亂碼問題弄了很久,一大早就開始想要怎么解決才好。
          很簡(jiǎn)單上傳頁面,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//            設(shè)置工廠
           8            DiskFileItemFactory factory = new DiskFileItemFactory();
           9            String path = request.getRealPath("/upload");
          10//            設(shè)置文件存儲(chǔ)位置
          11            factory.setRepository(new File(path));
          12//            設(shè)置大小,如果文件小于設(shè)置大小的話,放入內(nèi)存中,如果大于的話則放入磁盤中
          13            factory.setSizeThreshold(1024*1024);
          14            
          15            ServletFileUpload upload = new ServletFileUpload(factory);
          16//            這里就是中文文件名處理的代碼,其實(shí)只有一行,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//                    判斷是不是上傳的文件,如果不是得到值,并設(shè)置到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//                        得到不要地址的文件名,不同的瀏覽器傳遞的參數(shù)不同,有的直接傳遞文件名,而又的把文件地址一起傳遞過來
          42//                        使用substring方法可以統(tǒng)一得到文件名而不得到文件位置
          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//            跳轉(zhuǎn)到顯示結(jié)果頁面
          58            request.getRequestDispatcher("upload/result2.jsp").forward(request, response);
          59    }

          60
          61}
          用EL表達(dá)式顯示輸出
          1  <body>
          2     用戶名:${requestScope.username } <br>
          3     密  碼:${requestScope.password } <br>
          4     文件1 :${requestScope.file1 }<br>
          5     文件2 :${requestScope.file2 }<br>
          6  </body>

          其實(shí)很簡(jiǎn)單的設(shè)置就可以把中文件上傳,并正確顯示正確的中文文件名。
          在網(wǎng)上找了一點(diǎn)資料,但是都寫得很少,沒有把完整的寫出來。
          所以把它寫出來,讓大家少走點(diǎn)彎路。

          posted on 2008-11-11 12:02 duduli 閱讀(31892) 評(píng)論(15)  編輯  收藏 所屬分類: java

          評(píng)論

          # re: fileupload上傳中文文件名時(shí)亂碼問題 2008-11-11 12:15 低調(diào)

          serheaderencoding -- set  回復(fù)  更多評(píng)論   

          # re: fileupload上傳中文文件名時(shí)亂碼問題 2008-11-11 12:23 duduli

          對(duì)。就是setheaderencoding(encoding)低調(diào)
            回復(fù)  更多評(píng)論   

          # re: fileupload上傳中文文件名時(shí)亂碼問題 2008-11-11 14:38 lyre

          恩,中文的問題,一直都是比較讓人困擾的。  回復(fù)  更多評(píng)論   

          # re: fileupload上傳中文文件名時(shí)亂碼問題[未登錄] 2011-06-22 11:34 Allen

          恩,謝謝了  回復(fù)  更多評(píng)論   

          # re: fileupload上傳中文文件名時(shí)亂碼問題 2012-05-09 13:17 12

          我草,還是沒有解決我的問題,我用的是SmartUpload上傳的。干。  回復(fù)  更多評(píng)論   

          # re: fileupload上傳中文文件名時(shí)亂碼問題 2013-01-17 17:28 8888

          終于搞定了,多謝!!!!!!!  回復(fù)  更多評(píng)論   

          # re: fileupload上傳中文文件名時(shí)亂碼問題 2013-03-04 09:34 pass

          與struts2結(jié)合時(shí) 那文件名還是亂碼啊!!  回復(fù)  更多評(píng)論   

          # re: fileupload上傳中文文件名時(shí)亂碼問題 2013-04-03 10:36 你兒吃草

          我頂起啊!解決了!  回復(fù)  更多評(píng)論   

          # re: fileupload上傳中文文件名時(shí)亂碼問題 2013-04-22 15:01 不明白

          我用的是jquery file upload上傳文件,亂碼還是沒有解決啊~知道的請(qǐng)告之~謝謝  回復(fù)  更多評(píng)論   

          # re: fileupload上傳中文文件名時(shí)亂碼問題 2013-07-05 17:25 JL___

          天哪好了太感謝了啊  回復(fù)  更多評(píng)論   

          # re: fileupload上傳中文文件名時(shí)亂碼問題[未登錄] 2014-04-02 16:23 aa

          謝謝  回復(fù)  更多評(píng)論   

          # re: fileupload上傳中文文件名時(shí)亂碼問題 2014-04-30 22:40 一半一半

          表示真的可行  回復(fù)  更多評(píng)論   

          # re: fileupload上傳中文文件名時(shí)亂碼問題 2014-09-12 19:00 陌路千里

          太感謝樓主了  回復(fù)  更多評(píng)論   

          # re: fileupload上傳中文文件名時(shí)亂碼問題 2015-05-24 15:55 qwqw

          太感謝樓主了   回復(fù)  更多評(píng)論   

          # re: fileupload上傳中文文件名時(shí)亂碼問題 2016-06-29 23:03 luguo

          太感謝樓主了,一段代碼就解決了!  回復(fù)  更多評(píng)論   

          <2016年6月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          導(dǎo)航

          統(tǒng)計(jì)

          公告

          welcome to my place.

          常用鏈接

          留言簿(5)

          我參與的團(tuán)隊(duì)

          隨筆分類

          隨筆檔案

          新聞分類

          石頭JAVA擺地?cái)們?/h3>

          搜索

          •  

          積分與排名

          • 積分 - 256302
          • 排名 - 220

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          @duduli
          主站蜘蛛池模板: 于田县| 嘉兴市| 辰溪县| 蒙山县| 土默特左旗| 临湘市| 高陵县| 弥渡县| 梁河县| 穆棱市| 满城县| 舟曲县| 太湖县| 芮城县| 喀喇| 阳江市| 壶关县| 施甸县| 吴桥县| 揭阳市| 车致| 海阳市| 龙陵县| 桦南县| 莱西市| 叙永县| 哈密市| 墨竹工卡县| 清丰县| 神农架林区| 南投市| 镇坪县| 米泉市| 叶城县| 林芝县| 济南市| 西乌珠穆沁旗| 合川市| 大宁县| 绵阳市| 漳浦县|