0

I would like to display text file into textarea in primefaces.

Following user define steps are:

  1. Click on Upload control (user can upload multiple files)
  2. I am fetching all uploaded files and displaying file name in radio button
  3. From radio buttons, User will select one of the file and that file will load into textarea.

While performing 3rd steps, I am getting following error

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)

Following is the code: Xhtml code:

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

Could help me to solve this error ?

Neerajkumar
  • 300
  • 4
  • 19
  • 2
    So the file is gone... Error seems clear to me...You should do something with the uploaded files these are temporarry files. Move them to some better location, or put them in a database or whatever... – Kukeltje Apr 21 '17 at 08:16
  • Possible duplicate of [How to use PrimeFaces p:fileUpload? Listener method is never invoked or UploadedFile is null](http://stackoverflow.com/questions/8875818/how-to-use-primefaces-pfileupload-listener-method-is-never-invoked-or-uploaded) – Kukeltje Apr 21 '17 at 08:23
  • The 'duplicate' contains a "please note" that explains your problem – Kukeltje Apr 21 '17 at 08:24

0 Answers0