文件上傳 使用commons-fileupload-1.4控制項及依賴的commons-io-2.6控制項 jsp頁面中內容 <form action="../servlet/FileUpdate" method="post" enctype="multipart/form-data"> <div al ...
文件上傳
使用commons-fileupload-1.4控制項及依賴的commons-io-2.6控制項
jsp頁面中內容
<form action="../servlet/FileUpdate" method="post" enctype="multipart/form-data"> <div align="center"> <fieldset style="width:80%"> <legend>上傳文件</legend><br/> <div align="left">上傳文件1</div> <div align="left"> <input type="file" name="file1"/> </div> <div> <div align='left'> <input type='submit' value="上傳文件"/> </div> </div> </fieldset> </div> </form>
servlet
1 public void doGet(HttpServletRequest request, HttpServletResponse response) 2 throws ServletException, IOException { 3 response.setContentType("text/html;charset=utf-8"); 4 PrintWriter out = response.getWriter(); 5 // Create a factory for disk-based file items 6 DiskFileItemFactory factory = new DiskFileItemFactory(); 7 // Configure a repository (to ensure a secure temp location is used) 8 ServletContext servletContext = this.getServletConfig().getServletContext(); 9 File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir"); 10 factory.setRepository(repository); 11 // Create a new file upload handler 12 ServletFileUpload upload = new ServletFileUpload(factory); 13 upload.setHeaderEncoding("utf-8"); 14 // Parse the request 15 String saveName = ""; 16 try { 17 List<FileItem> items = upload.parseRequest(request); 18 for (FileItem item : items) { 19 if(item.isFormField()){//如果只是表單中信息,不是表單文件 20 String fieldName = item.getFieldName(); 21 String fieldValue = item.getString(); 22 out.print("<br>fieldName: "+fieldName+",--fieldValue: "+fieldValue); 23 }else{ 24 InputStream inputStream = item.getInputStream(); 25 //得到保存文件的路徑 26 String realpath=this.getServletContext().getRealPath("update"); 27 //得到上傳的文件的名字,可能顯示的是路徑,所以需要取出文件名 28 String allFilePath = item.getName(); 29 //getName()值為絕對路徑!!!下麵代碼轉換取文件名 30 String fileName = null; 31 int ind = allFilePath.lastIndexOf("\\"); 32 if (ind != -1) { 33 fileName = allFilePath.substring(ind + 1); 34 }else { 35 fileName = allFilePath; 36 } 37 38 out.print("<br>上傳的文件名: "+fileName); 39 //讀取的不能是目錄,應該加上文件名 40 File file=new File(realpath + "\\" + fileName); 41 if(file.getParentFile().exists()){ 42 file.createNewFile();//創建文件 43 }else { 44 file.getParentFile().mkdirs();//創建父級文件路徑 45 file.createNewFile();//創建文件 46 } 47 FileOutputStream fos=new FileOutputStream(file); 48 byte[] bytes= new byte[1024]; 49 int len=0; 50 //寫入文件 51 while((len=inputStream.read(bytes))!=-1){ 52 fos.write(bytes, 0, len); 53 } 54 55 inputStream.close(); 56 fos.close(); 57 out.print("<h3>"+allFilePath+"文件上傳成功</h3>"); 58 } 59 } 60 61 62 } catch (FileUploadException e) { 63 e.printStackTrace(); 64 } catch (FileNotFoundException e) { 65 e.printStackTrace(); 66 System.out.println(saveName); 67 } 68 }
web.xml
<servlet> <servlet-name>FileUpdate</servlet-name> <servlet-class>com.oneself.shopping.servlet.FileUpdate</servlet-class> </servlet> <servlet-mapping> <servlet-name>FileUpdate</servlet-name> <url-pattern>/servlet/FileUpdate</url-pattern> </servlet-mapping>
問題
java.io.FileNotFoundException: D:\update (拒絕訪問。)
FileOutputStream讀取流的時候如果是文件夾,就會出此錯誤。讀取的目錄後面要加文件名,如下:
1 File file=new File(realpath + "\\" + fileName); 2 if(file.getParentFile().exists()){ 3 //file.getParentFile().mkdirs();//創建父級文件路徑 4 file.createNewFile();//創建文件 5 System.out.println(file.exists()); 6 }else { 7 file.getParentFile().mkdirs();//創建父級文件路徑 8 file.createNewFile();//創建文件 9 } 10 FileOutputStream fos=new FileOutputStream(file);