0

I need to store profile pic of users on my application on the server side. I am storing that in the fileSystem through JSF(with Primefaces). However the documents are currently being stored in the tmp folder but get deleted after each restart of the server .

How should I store the documents which need to be kept permanently ?


I have been supplying the directory path through web.xml like follows: Do I just need to change the path to permanent location on the server?

(I am using the Primefaces uploadFile component to facilitate this uploading)

<filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>
        org.primefaces.webapp.filter.FileUploadFilter
    </filter-class>
    <init-param>
        <param-name>thresholdSize</param-name>
        <param-value>51200</param-value>
    </init-param>
    <init-param>
        <param-name>uploadDirectory</param-name>
        <param-value>/tmp</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
Rajat Gupta
  • 25,853
  • 63
  • 179
  • 294

1 Answers1

1

Store it on a permanent location instead of a temporary location. Supply an absolute disk file system path to a prepared folder with sufficient read/write rights for Java. You can if necessary make the path configureable as a system property or by a properties file. E.g. as a system property (which you specify as VM argument):

-Dfiles.location=/path/to/files

Then you can locate the file by:

File file = new File(System.getProperty("files.location"), filename);
// ...

Alternatively, you can store it in the DB, but storing binary data in DBs is often frowned upon as it's not indexable/searchable/linkable and thus defeats the sole purpose of storing in DB.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks BalusC. please revisit my edited question. I was configuring the path through web.xml. Could I just change it over there ? – Rajat Gupta Nov 15 '11 at 20:39
  • 1
    No, that's the place where files should be temporarily stored whenever they exceed the threshold size (otherwise the server's memory will blow up). You need to write it to a more permanent location yourself. Get the `InputStream` of the uploaded file and write it to `OutputStream` of the permanent file. You can ensure uniqueness of filename using `File#createTempFile(prefix, suffix, folder)`. See also e.g. [this](http://stackoverflow.com/q/4345754) and [this](http://stackoverflow.com/q/4817825). – BalusC Nov 15 '11 at 20:41