標籤: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上傳多個檔案(亂碼解決)