将文件从primefaces中的上传文件加载到textarea中

时间:2017-04-21 07:14:12

标签: file-upload primefaces jsf-2 filenotfoundexception

我想在primefaces中将文本文件显示到textarea中。

以下用户定义步骤为:

  1. 点击上传控件(用户可以上传多个文件)
  2. 我正在获取所有上传的文件并在单选按钮
  3. 中显示文件名
  4. 从单选按钮,用户将选择文件和该文件之一 将加载到textarea。
  5. 执行第3步时,我收到以下错误

    java.io.FileNotFoundException: C:\Users\UserName\Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\ProjectDummy\upload_d8a75697_ad88_43fe_acd8_8133bf93d727_00000017.tmp (The system cannot find the file specified)
    
    javax.servlet.ServletException: java.io.FileNotFoundException: C:\Users\UserName\Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\ProjectDummy\upload_d8a75697_ad88_43fe_acd8_8133bf93d727_00000017.tmp (The system cannot find the file specified)
            javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
            org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    

    以下是代码: Xhtml代码:

    <h:form method="POST" enctype="multipart/form-data"
                        prependId="false">
                        <p:growl id="messages" showDetail="true" />
                        <p:panelGrid columns="1">
                            <p:fileUpload
                                fileUploadListener="#{backingBean.handleFileUpload}"
                                mode="advanced" update="messages,console" auto="true"
                                multiple="true" />
                            <br />
                            <br />
    
                            <p:selectOneRadio id="console" value="#{backingBean.fileName}"
                                layout="grid" columns="1">
                                <f:selectItems value="#{backingBean.fileNames}" var="f"
                                    itemLabel="#{fileName}" itemValue="#{fileName}" />
    
                            </p:selectOneRadio>
    
                            <p:commandButton value="Load" action="#{backingBean.loadFile}"
                                ajax="false" update="txtInput" process="@all" style="width:80px"></p:commandButton>
                            <br />
                            <br />
                            <p:inputTextarea id="txtInput" value="#{backingBean.fileText}"
                                rows="10" cols="50">
                            </p:inputTextarea>
                        </p:panelGrid>
    
                    </h:form>
    

    BackingBean.java

    public void handleFileUpload(FileUploadEvent event) throws IOException, NotSerializableException {
    
            li = new ArrayList<UploadedFile>();
            li.add(event.getFile());
            setLi(li);
            ListIterator<UploadedFile> list = li.listIterator();
            while (list.hasNext()) {
                String str = event.getFile().getFileName();
                System.out.println(str);
                ++count;
                fileNames.add(str);
                fileList.add(event.getFile());
                list.next();
            }
        }
    
        public void loadFile() throws IOException, NotSerializableException {
            String filenameRadio = getFileName();
            for (int i = 0; i < count; i++) {
                String fileS = fileList.get(i).getFileName();
    
                if (fileS.equalsIgnoreCase(filenameRadio)) {
                    setUploadedFile(fileList.get(i));
                    InputStream is = getUploadedFile().getInputstream();
                    int read = 0;
                    byte[] bytes = new byte[(int) fileList.get(i).getSize()];
    
                    String s1;
                    while ((read = is.read(bytes)) != -1) {
                        s1 = new String(fileList.get(i).getContents());
                        setFileText(s1);
                        System.out.println(getFileText());
                    }
                    is.close();
                }
            }
        }
    

    可以帮我解决这个错误吗?

0 个答案:

没有答案