user644745
user644745

Reputation: 5713

JSF: how can i save stream as file outside of webapp and how to access it when deployed as war

I have couple of questions here.

  1. I am using the following to write a stream to a file. I want to write to outside the webapp container, as on a redeploy all the files will be deleted.

                InputStream in = event.getFile().getInputstream();    
                OutputStream out = new FileOutputStream(path + fName);
    
    
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
    
  2. I do not know if we can achieve this by externalContext of facesContext. If so, please give me an example of how to do it.

  3. How can I access this ? example, if i have to use it as follows, what path do i have to use?

    EmailAttachment attachment = new EmailAttachment(); attachment.setPath("mypictures/john.jpg");

Thanks in advance !

Upvotes: 1

Views: 1126

Answers (1)

AlexR
AlexR

Reputation: 115378

You are doing everything right. Use (for example) user tmp directory to write this file (System.getProperty("user.tmp.dir")). To access this file use FileInputStream in exactly the same manner as you are using FileOutputStream.

Upvotes: 3

Related Questions