Search code examples
javajsffile-uploadprimefacesreadfile

How to uploaded and read text file in JSF and PrimeFaces?


I need to upload and read a text file with PrimeFaces and JSF. My question is that when I uploaded the text file, where is it stored?

Here is my .xhtml file:

<p:fileUpload value="#{send.file }" mode="simple" />
</h:form>
<p:commandButton actionListener="#{send.upload}"  value="Send" ajax="false" />

And Java class:

public class Send {
    private UploadedFile file;

    public void upload() {
        if (file != null) {
            FacesMessage msg = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
}

I also found this example to read the file:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReaderExample {

    public static void main(String[] args) {

        try (BufferedReader br = new BufferedReader(new FileReader("C:\\testing.txt")))
        {
            String sCurrentLine;

            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

My other question is in this example "C:\\testing.txt" is given as a path? Which address I must give to read my uploaded file?


Solution

  • when I uploaded the text file, where is it stored?

    This is actually none of your business and you should not be interested in that from inside your JSF backing bean code. It's stored (partial) in memory and/or (partial) in server's temporary storage location which will be wiped/cleaned at intervals. It's absolutely not intented as permanent storage location. You should in the action/listener method just read the uploaded file content and store it in the permanent storage location to your choice.

    E.g.

    private static final File LOCATION = new File("/path/to/all/uploads");
    
    public void upload() throws IOException {
        if (file != null) {
            String prefix = FilenameUtils.getBaseName(file.getName()); 
            String suffix = FilenameUtils.getExtension(file.getName());
            File save = File.createTempFile(prefix + "-", "." + suffix, LOCATION);
            Files.write(save.toPath(), file.getContents());
            // Add success message here.
        }
    }
    

    Note that the FilenameUtils is part of Apache Commons IO which you should already have installed as it's a required dependency of <p:fileUpload>. Also note that File#createTempFile() does in above example not exactly generate a temp file, but it's just been used to generate an unique filename. Otherwise, when someone else coincidentally uploads a file with exactly the same name as an existing one, it would be overwritten. Also note that Files#write() is part of Java 7. If you're still on Java 6 or older, grab Apache Commons IO IOUtils instead.