我要上傳的圖片保存到特定的文件夾中的部署在Tomcat的Spring MVC的保存上傳MultipartFile到特定的文件夾
我的問題一個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")
,但沒有任何成功。
如何使用多部分文件的內容在應用程序的特定文件夾中創建新文件?
你得到什麼錯誤,當你試圖context.getRealPath或getResources(」目的地「)看到http://stackoverflow.com/questions/2970643/create-a-file-and-store-in-java-web-application-folder –
啊好吧謝謝,getRealPath沒有工作,因爲它返回null如果應用程序被部署到戰爭文件中並且未被提取出來!它在戰爭容器外工作!但是有什麼選擇可以在戰爭中獲得文件的路徑嗎? – Alexander
我不會這麼說。將文件添加到部署的戰爭中並沒有什麼意義。那是你想要做的嗎? –