0

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?

Ihor Patsian
  • 1,288
  • 2
  • 15
  • 25
Fatih
  • 59
  • 1
  • 4
  • 11
  • you are having the file object in upload() and use the file object then read the file line by line, using the BufferedReader – newuser Sep 04 '13 at 09:23
  • 1
    try this sample http://www.java-tutorial.ch/java-server-faces/file-upload-with-primefaces it will help you – newuser Sep 04 '13 at 09:29

3 Answers3

4

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

please take a look at this thread that is related to the same issue: how to upload file to http remote server using java?.

Please if it does not help you, let me know, and I will go through. ;)

Community
  • 1
  • 1
John Velandia
  • 129
  • 1
  • 4
0

I red the file this way

private UploadedFile file;

public void upload() {
    if (file != null && !"".equals(file.getFileName())) {
        try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(file.getInputstream(), "UTF-8"))) {
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }

        } catch (Exception ex) {
            LOG.error("Error uploading the file", ex);
        }
    }
}
fjkjava
  • 1,414
  • 1
  • 19
  • 24