Spring MVC將上傳的MultipartFile儲存到特定資料夾
我想將上傳的影象儲存到部署在Tomcat上的Spring 3 MVC應用程式中的特定資料夾中
我的問題是我無法將上傳的影象檔案儲存到執行應用程式的主機.
這是我試過的:
private void saveFile(MultipartFile multipartFile, int id) throws Exception { String destination = "/images/" + id + "/"+ multipartFile.getOriginalFilename(); File file = new File(destination); multipartFile.transferTo(file); }
結果:FileNotFoundException– 是的確,我想要建立這個檔案!
我嘗試使用context.getRealPath或getResources(“destination”),但沒有任何成功.
如何使用我的多部分檔案的內容在我的應用程式的特定資料夾中建立一個新檔案?
String filePath = request.getServletContext().getRealPath("/"); multipartFile.transferTo(new File(filePath));
程式碼日誌版權宣告:
翻譯自:http://stackoverflow.com/questions/10847994/spring-mvc-save-uploaded-multipartfile-to-specific-folder