【问题标题】:Save image to java project path using multipart使用多部分将图像保存到java项目路径
【发布时间】:2016-10-29 21:11:47
【问题描述】:

尝试将图像直接保存在项目路径中。图像直接从基于 Angularjs 构建的 UI 上传。我想将图像保存到 Eclipse 项目中的文件夹中,但它抛出异常。我正在使用spring框架的MutipartFile接口

项目结构

web
|
 src
   |
    main
       |
        webapp
             |
              app
                |
                 images

代码

@RequestMapping(path = "/attachmentPath", method = RequestMethod.POST)
    public Response attachmentPath(Attachment attachment , @RequestParam(value = "file") MultipartFile file) throws IllegalStateException, IOException {
String orgName = file.getOriginalFilename();
String filePath = "/images/"+orgName;
        File dest = new File(filePath);
        try { file.transferTo(dest); }
        catch (IllegalStateException e) 
        { e.printStackTrace();}

例外

java.io.FileNotFoundException: img\images\users\TestCopy1.jpg (The system cannot find the path specified)

【问题讨论】:

  • 哪个例外?提供堆栈跟踪?
  • @javaguy - 现已添加

标签: java angularjs eclipse spring


【解决方案1】:

正如异常所说,filePath 变量未解析为完整的文件夹路径,因此您需要修复您的 filePath 变量以解析为完整的绝对路径 URL,如下所示(例如)。

web应用文件夹绝对路径可以检索为request.getSession().getServletContext().getRealPath()

@RequestMapping(path = "/attachmentPath", method = RequestMethod.POST)
public Response attachmentPath(HttpServletRequest request, Attachment attachment , @RequestParam(value = "file") MultipartFile file) throws IllegalStateException, IOException {
    String orgName = file.getOriginalFilename();
    String absolutePathToImages = request.getSession().getServletContext().getRealPath("/images/");
    String filePath = absolutePathToImages + orgName;
    File dest = new File(filePath);
    //check destination exists, if not create it
    if(!dest.exists())
    {
       new File(dest).mkdir();
    }
    try { 
       file.transferTo(dest); 
    }
    catch (IllegalStateException e) 
    { 
        e.printStackTrace();
    }

【讨论】:

  • 我知道这个解决方案,并且我已经实现了它,但我要求的是直接将文件保存在项目中,而不使用绝对路径。假设如果我想从同一个文件夹中检索一张图像,我可以像这样检索它 - String rootDir = "images\\example.jpg"。以同样的方式,我想在没有绝对路径的情况下保存。我希望我能够描述我的问题。
  • 问题已部分解决。获取我的工作区的绝对路径。我的代码在不同的文件夹中。当我尝试找到该文件夹​​时,它给我的是工作区位置,而不是代码所在的代码位置。我们也可以解决这个问题吗?
  • G:\myProj\Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\web\img
【解决方案2】:

您的 String filePath = ... 没有 session 也没有 ServletContext 。这是抛出FileNotFoundException 异常。 .

这是我如何实现我的应用程序以将文件直接保存在项目中。您可以根据您的相应更改路径(根目录)。

我的 Pojo 类产品:

public class Product implements Serializable{

...

@JsonIgnore 
    private MultipartFile  productImage;

//getters setters

@XmlTransient  
    public MultipartFile getProductImage() {
        return productImage;
    }

    public void setProductImage(MultipartFile productImage) {
        this.productImage = productImage;
    }
...

}

我的控制器

@RequestMapping(value = "/add", method = RequestMethod.POST)
    public String processAddNewProductForm(@ModelAttribute("newProduct") @Valid Product productToBeAdded, BindingResult result, HttpServletRequest request) {   

        MultipartFile productImage = productToBeAdded.getProductImage();
        String rootDirectory = request.getSession().getServletContext().getRealPath("/");

            if (productImage!=null && !productImage.isEmpty()) {
               try {

                productImage.transferTo(new File(rootDirectory+"resources\\images\\"+productToBeAdded.getProductId() + ".png"));
               } catch (Exception e) {
                throw new RuntimeException("Product Image saving failed", e);
           }
           }


       //   productService.addProduct(productToBeAdded); 
        return "redirect:/products";
    }

【讨论】:

  • 问题已部分解决。获取我的工作区的绝对路径。我的代码在不同的文件夹中。当我尝试找到该文件夹​​时,它给我的是工作区位置,而不是代码所在的代码位置。我们也可以解决这个问题吗?
  • G:\myProj\Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\web\img
猜你喜欢
  • 1970-01-01
  • 2020-08-22
  • 1970-01-01
  • 2021-12-25
  • 2021-02-07
  • 2019-05-02
  • 1970-01-01
  • 2012-07-26
  • 2014-06-07
相关资源
最近更新 更多