我的文件夾結構是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);
}
}
目前運行'try {}'會發生什麼?你有例外嗎? – halfer