使用Spring MVC将文件写入文件夹文件夹和文件位置之间的区别

时间:2016-10-26 01:50:42

标签: java eclipse spring spring-mvc

我目前正在尝试使用Spring MVC将图像写入文件夹。当我试图写入文件夹时,我看不到图像。我认为问题是写目录和项目位置不匹配。我的项目结构如下enter image description here

我的文件夹位置为enter image description here

我添加代码的控制器是

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

    productDao.addProduct(product);

    MultipartFile productImage = product.getProductImage();
    //creates http path of the real path
    String rootDirectory = request.getSession().getServletContext().getRealPath("/");
    //creates a dynamic path from the real path
    path = Paths.get(rootDirectory+product.getProductId()+".png");
    System.out.println(rootDirectory+product.getProductId()+".png");
    System.out.println(path);
    // if product Image is Empty it will transfer the file type to the new file type PNG
    if (productImage !=null && productImage.isEmpty()){

        try {
            productImage.transferTo(new File(path.toString())); 

        } catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("Project saving failed",e);



        }

我可以从我的控制台看到控制器认为根目录

C:\用户\特雷弗\ workspace_jsp.metadata.plugins \ org.eclipse.wst.server.core \ TMP0 \ wtpwebapps \ HelloSpring3 \

所以我的问题是如何修复控制器位置和我要写入的位置之间的不匹配?提前谢谢。

0 个答案:

没有答案