Reputation: 511
I am getting the same problem here is my code
@RequestMapping(value = "/admin/productInventory/addProduct", method = RequestMethod.POST)
public String addProductInInventory(@ModelAttribute("product") Product product, HttpServletRequest request) {
productDao.addProduct(product);
MultipartFile productImage = product.getProductImage();
String rootDirectory = request.getSession().getServletContext().getRealPath("/ ");
System.out.println("rootDirectory >>>"+rootDirectory);
path = Paths.get(rootDirectory + "\\WEB-INF\\resources\\images\\" +product.getProductName()+"_"+product.getProductId());
if(productImage != null && !productImage.isEmpty()) {
try{
productImage.transferTo(new File(path.toString()));
System.out.println("actual path>>>" +path.toString());
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException("Product image saving faild.");
}
}
return "redirect:/admin/productInventory";
}
now the file is getting saved at path >>
C:\Users\rajan\Documents\workspace-sts-3.7.3.RELEASE.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\MusicStore\WEB-INF\resources\images\
but uploaded file isn't showing in webapps/resources/imaged in package explorer in my ide.
Upvotes: 0
Views: 298
Reputation: 407
It's probably because you're running your project directly from eclipse. When you do so, this is the location where our WAR
resides(inside your workspace). This path is used by eclipse. So in this case, C:\Users\rajan\Documents\workspace-sts-3.7.3.RELEASE.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\MusicStore
is the context base path. That's why file is stored there. Try running tomcat from terminal and you'll get it at the desired location.
Upvotes: 4