星期五, 8月 05, 2011

[Java] Servlet file upload filename encoding (中文亂碼)

解決中文檔名亂碼問題:在init ServletFileUpload呼叫setHeaderEncoding指定編碼為utf-8,
就能正確取出中文了

ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("utf-8");
List items = null;
   try {
    items = upload.parseRequest(request);
//    System.out.println("item is added.");
   } catch (FileUploadException e) {
    System.out.println("FileUploadException:" + e.getMessage());
   }
   if (items != null) {
//    System.out.println("items count:" + items.size());
    Iterator iter = items.iterator();
    while (iter.hasNext()) {
                                  FileItem item = iter.next();
                                  String filename = item.getName();
                                }
                        }
Reference:
servlet file upload filename encoding

沒有留言:

張貼留言

留個話吧:)