0

Hi everyone I'm getting this error when I try to upload files to apache tomcat using Primefaces 5.2 FileUpload. I need to upload several images and this don´t work.

java.io.FileNotFoundException: /Users/Desarrollo/Desktop/projects_JEE/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/TEO/WebContent/resources/images/Captura de pantalla 2015-07-09 a las 18.09.51.png (No such file or directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:171)
at mx.com.teo.view.HotelView.uploadImages(HotelView.java:105)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.el.parser.AstValue.invoke(AstValue.java:247)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at org.primefaces.component.fileupload.FileUpload.broadcast(FileUpload.java:310)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:755)
at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:931)
at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at mx.com.teo.filter.AuthorizationFilter.doFilter(AuthorizationFilter.java:46)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

This is part of my xhtml

<p:tab id="images" title="#{msg.extrasLabel}">
                    <p:panel header="#{msg.imagesUploadLabel}">
                        <p:messages />
                        <h:panelGrid columns="2" columnClasses="label, value">
                            <p:fileUpload fileUploadListener="#{hotelView.uploadImages}"
                      mode="advanced"
                      update="messages"
                      label="Choose a file"
                      sizeLimit="10485760"
                      multiple="true"
                      allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
                      invalidSizeMessage="The maximum file size allowed is 10 Megabytes !"
                      invalidFileMessage="You are allowed to upload only images !"
                      />
        <p:growl id="messages" showDetail="true" life="5000"/>
                        </h:panelGrid>
                    </p:panel>
                </p:tab>

And this is my method

public void uploadImages(FileUploadEvent event){

    ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();

    File result = new File(extContext.getRealPath("/WebContent/resources/images/" + event.getFile().getFileName()));
    System.out.println(extContext.getRealPath("/WebContent/resources/images/" + event.getFile().getFileName()));
    try{
        FileOutputStream fileOutputStream = new FileOutputStream(result);
        byte[] buffer = new byte[BUFFER_SIZE];
        int bulk;
        InputStream inputStream = event.getFile().getInputstream();
        while(true){
            bulk = inputStream.read(buffer);
            if(bulk < 0){
                break;
            }
            fileOutputStream.write(buffer, 0, bulk);
            fileOutputStream.flush();
        }
        fileOutputStream.close();
        inputStream.close();

        FacesMessage msg = new FacesMessage("File Description", "file name: " + event.getFile().getFileName() +
                "<br/> file size: " + event.getFile().getSize() / 1024 + "Kb<br/>content type: " + 
                event.getFile().getContentType() + "<br/><br/>The file was uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    } catch(IOException e){
        e.printStackTrace();
        FacesMessage error = new FacesMessage(FacesMessage.SEVERITY_ERROR, "The files were not uploaded!", "");
        FacesContext.getCurrentInstance().addMessage(null, error);
    }
}
EGonzalez
  • 1
  • 2
  • Because of filename "Captura de pantalla 2015-07-..." avoid all special chars and free spaces. – Stefan Jul 31 '15 at 17:01
  • @Stefan I rename the file to **Captura.png** and the error is the same – EGonzalez Jul 31 '15 at 17:07
  • Moving uploaded documents to that location is bad practice due to uncertainty of availability of that same folder after redeploys or restarts. Upload them to a more stable location and serve them via a servletfilter – Kukeltje Jul 31 '15 at 17:11
  • With 'that' location I mean the externalContext.getRealPath() location – Kukeltje Jul 31 '15 at 17:19
  • Misleading title. You can upload it perfectly. You could only not save it due to a severe mistake in code logic. – BalusC Jul 31 '15 at 17:29
  • is that wrong??? I want to save de images in TEO/WebContent/resources/images TEO is my proyect's folder – EGonzalez Jul 31 '15 at 17:30
  • You tried to save it in WAR's deploy folder. This makes no sense. It would get lost everytime you redeploy the WAR. See the duplicate for the right way (and the link therein for further explaination why getRealPath() is bad) – BalusC Jul 31 '15 at 17:31
  • now I understand, sorry for the duplicated question – EGonzalez Jul 31 '15 at 18:01

0 Answers0