servlet上傳多個檔案(亂碼解決)

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   ar   color   os   使用   

首先,建議將編碼設定為GB2312,並在WEB-INF\lib裡匯入:commons-fileupload-1.3.jar和commons-io-2.4.jar,

可百度下下載,然後你編碼完成後,上傳時可能會遇到"servlet Bad version number in .class file"錯誤。

解決:

1.Window --> Preferences -->Java --> compiler中的compiler compliance level對應的下拉式功能表中選擇JDK版本.

2.Window --> Preferences -->MyEclipse --> Servers-->Tomcat --> Tomcat n.x -->JDK中的Tomcat JDKname下的下拉式功能表中選擇自己電腦上安裝的JDK版本(必須與步驟1中的JDK版本一致).

如果還是沒有解決,不用著急,因為有些MyEclipse版本內建有JDK版本,所以也要將它改過來.

3.Window --> Preferences -->Java -->Installed JRES,然後在右邊選擇與步驟1和2版本一致的JDK版本,如果沒有,可以自己添加.然後選中就可以了.

4、.Window --> Preferences -->MyEclipse --> Servers-Resin 3-Resin 3.x-JDK-Resin jdk name:選擇jdk1.6.0_03

1.前台jsp:

enctype="multipart/form-data"

  <body style="margin:50px">  <form action="servlet/UploadServlet" method="post" enctype="multipart/form-data">     <p>上傳檔案:</p>     檔案1:<input type="file" name="file1" /><br/>     描述:<input type="text" name="description1" /><br/>     檔案2:<input type="file" name="file2" /><br/>    描述:<input type="text" name="description2" /><br/>  <input type="submit" value=" 上  傳 " />    </form>  </body>  

2.後台action:

public class UploadServlet extends HttpServlet {           public void doGet(HttpServletRequest request, HttpServletResponse response)              throws ServletException, IOException {           response.setCharacterEncoding("GB2312");           response.getWriter().println("<script>alert(‘請用POST方式上傳檔案!‘)</script>");      }        public void doPost(HttpServletRequest request, HttpServletResponse response)              throws ServletException, IOException {          File file=null;          String description=null;                    String uploadPath=this.getServletContext().getRealPath("upload");          String uploadTemp=this.getServletContext().getRealPath("upload/temp"); 
//設定響應格式(不佈建要求格式,因為檔案是二進位的,不能使用UTF-8格式化) response.setContentType("text/html;charset=gb2312"); PrintWriter out=response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println("<HEAD><TITLE>檔案上傳</TITLE></HEAD>"); out.println("<BODY style=‘margin:50px‘>"); out.println("上傳日誌:<BR/>");
//建立基於磁碟的工廠,針對大檔案,臨時檔案將儲存在磁碟 DiskFileItemFactory factory=new DiskFileItemFactory(); //設定緩衝區大小,超出該檔案直接寫入到磁碟的大小設定門檻。 factory.setSizeThreshold(10240); //這裡預設10KB //設定用於大於配置的大小閾值設定的臨時隱藏檔目錄。 factory.setRepository(new File(uploadTemp)); //建立一個檔案上傳的控制代碼 ServletFileUpload upload=new ServletFileUpload(factory); //設定最大檔案尺寸 ,這裡是4MB upload.setSizeMax(4194304); upload.setHeaderEncoding("GB2312"); try { //將解析結果放在LIST中 List<FileItem> list =upload.parseRequest(request); out.println("遍曆所有的 FileItem ... <br/>"); // 遍曆 list 中所有的 FileItem for(FileItem item:list) { // 如果是 文本域 if(item.isFormField()) { if(item.getFieldName().equals("description1")||item.getFieldName().equals("description2")) { description = item.getString("GB2312"); out.println("遍曆到 "+item.getFieldName()+" ... <br/>"+description+"<BR/>"); } } else { //否則為檔案域,當getName為Null說明沒有選則檔案 if((item.getFieldName().equals("file1")||item.getFieldName().equals("file2")) &&item.getName()!=null&&!item.getName().equals("")) { try { File remoteFile=new File(item.getName()); out.println("遍曆到 file ... <br/>"); out.println("用戶端檔案位置: " + remoteFile.getAbsolutePath() + "<br/>"); // 伺服器端檔案,放在 upload 檔案夾下 file=new File(uploadPath,remoteFile.getName()); if(!file.getParentFile().exists()) file.getParentFile().mkdirs(); if(!file.exists()) file.createNewFile(); item.write(file); } catch (Exception e) { } finally //總是立即刪除儲存表單欄位內容的臨時檔案 { item.delete(); } } } } out.println("Request 解析完畢,檔案上傳完畢!"); } catch (Exception e) { out.println("Request 解析異常!"+e.getMessage()); } out.flush(); out.close(); }
}

web.xml

建立servlet的時候,會自動設定檔案  

剖析Commons-fileupload.jar上傳原理,解決中文亂碼原因  

servlet上傳多個檔案(亂碼解決)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.