java.io.FileNotFoundException使用apache.commons

时间:2016-07-01 11:16:19

标签: java eclipse spring

我正在开发一个Spring-MVC项目,该项目需要应用程序的用户上传图像。我正在使用apache.commons,Eclipse Mars2和Java1.8。每次我测试上传时,eclipse控制台都会抛出这个:

编辑:似乎问题是找到独立于操作系统的项目路径。目前我们正在使用:

File localFile = new File(System.getProperty("user.home")+File. separator+ "workspace"+File. separator+"ProyectoSpring"+File. separator + "WebContent" + File. separator+"usr"+File. separator+imagen.getNombreImagen());

问题是工作区是硬编码的,应用程序正由更多人开发。有没有办法找到项目的路径? user.dir返回eclipe路径,而不是项目或工作区。

原始问题:

java.io.FileNotFoundException: C:\Users\UserName\workspace\ApacheTomcat7\ProyectoSpring\WebContent\usr\back.jpg (El sistema no puede encontrar la ruta especificada)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at controller.ImagenController.grabarFicheroALocal(ImagenController.java:84)
at controller.ImagenController.processAddSubmit(ImagenController.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:919)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:851)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

控制器方法:

@RequestMapping(value="/uploadImagen" , method=RequestMethod.GET) 
public String addImagen(Model model) {
    model.addAttribute("imagen", new Imagen());
    return "imagen/uploadImagen";
}

@RequestMapping(value="/uploadImagen", method=RequestMethod.POST) 
public String processAddSubmit(@ModelAttribute("imagen") Imagen imagen,
                                BindingResult bindingResult) { 
    //if (bindingResult.hasErrors())
    //      return "imagen/uploadImagen";
    //

    try {

        String aux = imagen.getFichero().getOriginalFilename();
        imagen.setImageName(System.getProperty("catalina.home")+ File. separator +"ProyectoSpring"+ File. separator +"WebContent"+ File. separator +"usr"+ File. separator + aux);
        grabarFicheroALocal(imagen);
    } catch (Exception e) {
        e.printStackTrace();
        return "user/error";
    }        
     return "redirect:../index.jsp";
 }


private void grabarFicheroALocal(Imagen imagen) throws Exception {

    CommonsMultipartFile uploaded = imagen.getFichero();
    File localFile = new File(imagen.getImageName());
    FileOutputStream os = null;

    try {
        os = new FileOutputStream(localFile);
        os.write(uploaded.getBytes());

    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

来自对象的摘录:

import org.springframework.web.multipart.commons.CommonsMultipartFile;



public class Imagen {

    private String imageName;
    private CommonsMultipartFile fichero;


public Imagen(){
    super();
}   

//GETTERS AND SETTERS

public CommonsMultipartFile getFichero() {
    return fichero;
}

public void setFichero(CommonsMultipartFile fichero) {
    this.fichero = fichero;
}
...

当然,图片未上传。在使用Linux的其他PC(我使用的是W7-64)中,代码可以工作,所以我怀疑它可能是权限问题,但路径中的所有文件夹都具有写权限。有任何想法吗? THX。

0 个答案:

没有答案