springMVC的檔案上傳於下載
阿新 • • 發佈:2018-11-02
springMVC的檔案上傳於下載
1、springmvc 檔案的上傳也是藉助於兩個工具所以需要新增兩個jar apache-commons-fileupload.jar apache-commons-io.jar 2、在spring-servlet.xml中新增檔案上傳的處理bean的配置。 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8" /> <!-- 預設編碼 (ISO-8859-1) --> <property name="maxInMemorySize" value="10240" /> <!-- 最大記憶體大小 (10240) --> <property name="uploadTempDir" value="/temp/" /> <!--(臨時檔案儲存目錄) 上傳後的目錄名 (WebUtils#TEMP_DIR_CONTEXT_ATTRIBUTE) --> <property name="maxUploadSize" value="-1" /> <!-- 最大檔案大小,-1為無限止(-1) --> </bean> 其中屬性<property name="uploadTempDir" value="/temp/" /> 的配置配置的是臨時檔案目錄,spring會將檔案先傳到臨時檔案,然後我們再呼叫對應的API將臨時檔案寫到目標檔案。 3、編寫上傳檔案的controller 3.1上傳一個檔案 直接在處理的方法中設定形參@RequestParam("file") CommonsMultipartFile file 注意這裡的引數必須使用@RequestParam指定。 然後呼叫方法file.getFileItem().write(targetFile);將臨時檔案寫出到目標檔案。 示例: /** * 上傳一個檔案 * @param name * @param file * @param session * @return */ @RequestMapping(value="/upload.do",method=RequestMethod.POST) public String fileUpLoad(String name,@RequestParam("file") CommonsMultipartFile file,HttpSession session){ if(!file.isEmpty()){ String path = session.getServletContext().getRealPath("/upload/"); String fileName = file.getOriginalFilename(); String fileType = fileName.substring(fileName.lastIndexOf(".")); File targetFile = new File(path,new Date().getTime()+fileType); try { file.getFileItem().write(targetFile); } catch (Exception e) { e.printStackTrace(); } } return "showData"; } 3.2上傳多個檔案 上傳多個檔案時,其實和上傳一個檔案一樣,只是將形參改為@RequestParam("file") CommonsMultipartFile[] file 然後我們只需在方法中迴圈處理這些檔案即可。 示例: /** * 上傳多個檔案 * @param name * @param files * @param session * @return */ @RequestMapping(value="/mupload.do",method=RequestMethod.POST) public String muFileUpLoad(String name,@RequestParam("file") CommonsMultipartFile[] files,HttpSession session){ if(files!=null && files.length>0){ String path = session.getServletContext().getRealPath("/upload/"); for (CommonsMultipartFile file : files) { String fileName = file.getOriginalFilename(); String fileType = fileName.substring(fileName.lastIndexOf(".")); File targetFile = new File(path,new Date().getTime()+fileType); try { file.getFileItem().write(targetFile); } catch (Exception e) { e.printStackTrace(); } } } return "showData"; } 4、檔案下載 檔案下載其實和spring沒關係,還是使用最普通的方式實現下載即可,在這裡不贅述。 示例: /** * 檔案下載 * @param session * @param response * @param fileName * @param isOnline * @throws Exception */ @RequestMapping(value="/downLoad.do",method=RequestMethod.GET) public void downLoad(HttpSession session,HttpServletResponse response,String fileName,boolean isOnline)throws Exception{ String path = session.getServletContext().getRealPath("/upload/")+"\\"+fileName; File file = new File(path); System.out.println(path); if(!file.exists()){ response.sendError(404, "您要下載的檔案沒找到"); return; } BufferedInputStream bufIn = new BufferedInputStream(new FileInputStream(file)); byte [] buff = new byte[1024]; int len = -1; response.reset(); if(isOnline){ URL u = new URL("file:///"+path); response.setContentType(u.openConnection().getContentType()); response.setHeader("Content-Disposition", "inline;filename="+fileName); }else{ response.setContentType("application/x-msdownload"); response.setHeader("Content-Disposition", "attachment;filename="+fileName); } OutputStream out = response.getOutputStream(); while((len=bufIn.read(buff))!=-1){ out.write(buff,0,len); out.flush(); } bufIn.close(); out.close(); }