2017-08-01 56 views
-1

我的文件夾結構是webapp-->resource--> images獲取圖像不保存到項目目錄

這裏是我的代碼:

public String addProductPost(@ModelAttribute("products") Product product, HttpServletRequest request) { 

     productDao.addProduct(product); 
     System.out.println(product); 

     MultipartFile productImage = product.getProductImage(); 
     String rootDirectory = request.getSession().getServletContext().getRealPath("/"); 
     path = Paths.get(rootDirectory +"webapp/resources/image"+ product.getProductId()+".png"); 

     if (productImage !=null && !productImage.isEmpty()) { 

      try { 
       productImage.transferTo(new File(path.toString())); 
       /*Files.copy(Paths.get(productImage.toString()), path);*/ 
      } catch (Exception e) { 
       e.printStackTrace(); 
       throw new RuntimeException("Product image saving failed ", e); 
      } 
     } 
+0

目前運行'try {}'會發生什麼?你有例外嗎? – halfer

回答

0

你嘗試System.out.println(path);?如果你沒有收到任何錯誤,那麼圖像將被保存在上述目錄中,但不會顯示在eclipse項目管理器中。要查看圖像,請導航到您說圖像的文件夾。

+0

我得到下面的錯誤,但upto路徑其顯示目錄,旁邊,它沒有保存到我的項目目錄webapp /資源/圖像java.io.FileNotFoundException:F:\我的音樂Web應用程序\ .metadata \ .plugins \ org。 eclipse.wst.server.core \ tmp0 \ wtpwebapps \ MusicStore \ webapp \ resources \ image \ 92.png(系統找不到指定的路徑) –

+0

您是否在Web.xml中添加了多部分bean?圖像文件夾是否存在於資源文件夾中?您是否手動轉到F:\路徑並檢查圖像是否存在? –