上传的文件没有保存在 webapps 的资源下

uploaded file isn't getting saved in under resources in webapps

我遇到了同样的问题,这是我的代码

@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";
    }

现在文件被保存在路径 >>

C:\Users\rajan\Documents\workspace-sts-3.7.3.RELEASE.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\MusicStore\WEB-INF\resources\images\

但是上传的文件没有显示在我 ide 的包资源管理器的 webapps/resources/imaged 中。

这可能是因为您 运行 您的项目直接来自 eclipse。当您这样做时,这就是我们 WAR 所在的位置(在您的工作区内)。此路径由 eclipse 使用。所以在这种情况下,C:\Users\rajan\Documents\workspace-sts-3.7.3.RELEASE.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\MusicStore 是上下文基本路径。这就是文件存储在那里的原因。从终端尝试 运行 tomcat,您将在所需位置获得它。