为什么无法使用primefaces将文件保存到服务器?

时间:2015-07-31 16:49:59

标签: jsf file-upload primefaces

大家好我在尝试使用Primefaces 5.2 FileUpload将文件上传到apache tomcat时出现此错误。 我需要上传几张图片,但这不起作用。

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)

这是我的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>

这是我的方法

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);
    }
}

0 个答案:

没有答案