2012-06-01 168 views
21

我要上傳的圖片保存到特定的文件夾中的部署在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.getRealPathgetResources("destination"),但沒有任何成功。

如何使用多部分文件的內容在應用程序的特定文件夾中創建新文件?

+0

你得到什麼錯誤,當你試圖context.getRealPath或getResources(」目的地「)看到http://stackoverflow.com/questions/2970643/create-a-file-and-store-in-java-web-application-folder –

+0

啊好吧謝謝,getRealPath沒有工作,因爲它返回null如果應用程序被部署到戰爭文件中並且未被提取出來!它在戰爭容器外工作!但是有什麼選擇可以在戰爭中獲得文件的路徑嗎? – Alexander

+0

我不會這麼說。將文件添加到部署的戰爭中並沒有什麼意義。那是你想要做的嗎? –

回答

20

此代碼一定會幫助你。

String filePath = request.getServletContext().getRealPath("/"); 
multipartFile.transferTo(new File(filePath)); 
+0

但沒有使用http servlet請求我們如何獲得路徑..? –

+0

有很多方法:您可以使用請求或會話來獲得真實路徑。謝謝拉維。 M – Phuong

6

,讓我們創建上傳目錄中的Web應用程序,並保存在webapp的文件/上傳

@RestController 
public class GreetingController { 

    private final static Logger log = LoggerFactory.getLogger(GreetingController.class); 

    @Autowired 
    private HttpServletRequest request; 


    @RequestMapping(value = "/uploadfile", method = RequestMethod.POST) 
     public 
     @ResponseBody 
     ResponseEntity handleFileUpload(@RequestParam("file") MultipartFile file) { 
      if (!file.isEmpty()) { 
       try { 
        String uploadsDir = "/uploads/"; 
        String realPathtoUploads = request.getServletContext().getRealPath(uploadsDir); 
        if(! new File(realPathtoUploads).exists()) 
        { 
         new File(realPathtoUploads).mkdir(); 
        } 

        log.info("realPathtoUploads = {}", realPathtoUploads); 


        String orgName = file.getOriginalFilename(); 
        String filePath = realPathtoUploads + orgName; 
        File dest = new File(filePath); 
        file.transferTo(dest); 

代碼
String realPathtoUploads = request.getServletContext().getRealPath(uploadsDir);

返回我一個路徑,如果我跑了來自Idea IDE的應用程序
C:\Users\Iuliia\IdeaProjects\ENumbersBackend\src\main\webapp\uploads\

和未來路徑,如果我做的.war和Tomcat下運行它:
D:\Programs_Files\apache-tomcat-8.0.27\webapps\enumbservice-0.2.0\uploads\

我的項目結構:
enter image description here

+0

代碼'file.transferTo(dest);'的最後一行給我IO異常,錯誤信息是「系統找不到指定的路徑」。任何想法爲什麼發生這種情況? –

+1

@AND,你確定這個路徑存在於你的文件系統中嗎?您需要首先創建目錄(手動或編程)。 –

1

我看到使用XML配置(注意,這確實彈簧3例不炒鍋彈簧4.2 *):http://www.devmanuals.com/tutorials/java/spring/spring3/mvc/spring3-mvc-upload-file.html `

<bean id="multipartResolver" 
 
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
 
<property name="maxUploadSize" value="100000" /> 
 
<property name="uploadTempDir" ref="uploadDirResource" /> 
 
</bean> 
 

 
<bean id="uploadDirResource" class="org.springframework.core.io.FileSystemResource"> 
 
<constructor-arg> 
 
<value>C:/test111</value> 
 
</constructor-arg> 
 
</bean>

0
String ApplicationPath = 
     ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath(""); 

這是如何讓應用程序在春天的真實路徑(未使用的響應,會議...)

+1

你不應該使用'getRealPath()'來保存文件..參見[this answer](https://stackoverflow.com/a/12160863/3891638) – cozyconemotel

相關問題