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

Fatih

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?

BalusC

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java Uploaded File Name Turkish Character Issue in JSF - Primefaces

From Dev

how can I read from an uploaded text file

From Dev

How to Read Contents of an Uploaded File

From Dev

How to read uploaded file details in Jersey

From Dev

jsf 2.0 Primefaces multiple Fileupload does not remember uploaded files

From Java

How to read a text file?

From Dev

AJAX file upload error in JSF/PrimeFaces : Cannot read property 'debug' of undefined

From Dev

How to delay Primefaces AjaxStatus on JSF?

From Dev

How to deactivate a JSF tag (Primefaces)

From Dev

how to connect knobs in primefaces jsf

From Dev

using JSF PrimeFaces' text editor, how to add text in PDF using iText

From Dev

how to read uploaded files

From Dev

How to read uploaded file that triggered lambda execution directly?

From Dev

Error downloading a File using JSF and Primefaces

From Dev

Jersey: read uploaded file as JSON

From Dev

Read uploaded JSON file in Rails

From Dev

How can I get uploaded text file in view through Django?

From Java

How to read a local text file?

From Dev

How to Read a text file continuosuly

From Dev

How to read a text file into a variable?

From Dev

How read text file on server?

From Dev

How to Read text file to DataTable

From Dev

How to Read a text file continuosuly

From Dev

How read text file on server?

From Dev

How to Write Unit Tests for Primefaces/JSF Apps

From Dev

How to validate a List<Object> in a Primefaces dataTable - JSF

From Dev

How to configure primefaces with existing JSF application?

From Dev

How to find unnamed component in jsf/primefaces

From Dev

How to set length of slider in Primefaces (JSF)?

Related Related

HotTag

Archive