gpt4 book ai didi

java - 如何将 getRealPath ("/") 路径从 .metadata 更改为 WEB-INF

转载 作者:太空宇宙 更新时间:2023-11-04 11:34:40 25 4
gpt4 key购买 nike

我正在从应用程序上传图像文件,现在我想将它们保存在 WEB-INF 中,但 eclipse 将其保存在 .metadata 文件夹.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps

请告诉我谁可以获取 WEB-INF3 的路径

@RequestMapping(value = "/admin/productInventory/addProduct", method = RequestMethod.POST)
public String addProduct(@ModelAttribute("product") Product product,HttpServletRequest request) {//httservlet- aswe need to use that toget thesessionpath
productDao.addProduct(product);
/////add image
MultipartFile productImage= product.getProductImage();
String rootDirectory= request.getSession().getServletContext().getRealPath("/"); //C:\Users\Avinash Kharche\ECommerce_Spring_Neon\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\emusicStore\
path =Paths.get(rootDirectory + "\\WEB-INF\\resources\\images\\" + product.getProductId()+".png");

//path =Paths.get("C:\\Users\\Avinash Kharche\\ECommerce_Spring_Neon\\emusicStore\\src\\main\\webapp\\WEB-INF\\resources\\images\\" + product.getProductId()+".png");


if(productImage!=null && !productImage.isEmpty()){
try{
productImage.transferTo(new File(path.toString()));
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException("Product image saving failed",e);

}
}
/////
return "redirect:/admin/productInventory";

}

我不想使用注释路径

(//path =Paths.get("C:\Users\Avinash Kharche\ECommerce_Spring_Neon\emusicStore\src\main\webapp\WEB-INF\resources\images\"+ Product.getProductId()+".png");)

请让我知道任何解决方案。谢谢

最佳答案

解决方案如下:

    @RequestMapping(value = "/admin/productInventory/addProduct", method = RequestMethod.POST)
public String addProductPost(@ModelAttribute("product") Product product, HttpServletRequest request) {
productDao.addProduct(product);

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

if (productImage != null && !productImage.isEmpty()) {
try {
productImage.transferTo(new File(path.toString()));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Product image saving failed", e);
}
}

return "redirect:/admin/productInventory";
}

更改路径:

path =Paths.get(rootDirectory + "\\WEB-INF\\resources\\images\\" + product.getProductId()+".png");

path = Paths.get(rootDirectory + "/WEB-INF/resources/images/" + product.getProductId() + ".png");

确保对编辑和删除功能执行相同的操作。我有同样的问题。使用您的版本,它可以在 Windows 上运行,但在 Mac 上,我必须更改如上所示的路径

关于java - 如何将 getRealPath ("/") 路径从 .metadata 更改为 WEB-INF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43413040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com