Eclipse Spring MVC文件上传问题

时间:2018-01-25 17:15:54

标签: java eclipse spring-mvc servlet-3.0

我已经尝试在Spring MVC中上传文件,在webcontnet目录下,当上传文件时它会默认eclipse工作空间目录,比如

  

eclipseworkspace / .metadata / .plugins / org.eclipse.wst.server.core / TMP0 / wtpwebapps /的webapp /图像/ Plastic_model.png

以下代码我试过了,

         String uploadDir = "/images/";
         String realPath = request.getServletContext().getRealPath(uploadDir);

         File transferFile = new File(realPath + "/" + file.getOriginalFilename()); 
         file.transferTo(transferFile);

我需要在Webcontent目录中上传文件,我如何解决这个问题

UPDATE1:

String paramValue = request.getSession().getServletContext().getInitParameter("upload.image.path");         
         String realPath = request.getServletContext().getRealPath(paramValue);          
         File transferFile = new File(realPath + "/" + file.getOriginalFilename()); 
         file.transferTo(transferFile);
web.xml中的

<context-param>
 <param-name>upload.image.path</param-name>
 <param-value>/home/robert/eclipse-workspace/Webapp/WebContent/images</param-value>
</context-param>

我找不到文件错误:

  

INFO:已完成名称为[/ Webapp]的重新加载上下文   java.io.FileNotFoundException:/home/robert/eclipse-workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/Webapp/home/robert/eclipse-workspace/Webapp/WebContent/images /Plastic_model.png(没有这样的文件或目录)

提前致谢

0 个答案:

没有答案