Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is data stored during streaming file upload via Apache Commons?

I saw this nifty guide on how to do streaming file uploads via Apache Commons. This got me thinking where is the data stored? And is it necessary to "close" or "clean" that location?

Thanks!

like image 971
user571099 Avatar asked Jul 11 '12 00:07

user571099


People also ask

What is ServletFileUpload in java?

public class ServletFileUpload extends FileUpload. High level API for processing file uploads. This class handles multiple files per single HTML widget, sent using multipart/mixed encoding type, as specified by RFC 1867.

What is Fileitem in Java?

This class represents a file or form item that was received within a multipart/form-data POST request. After retrieving an instance of this class from a FileUpload instance (see #parseRequest(javax. servlet. http.


2 Answers

where is the data stored?

I don't think it is stored. The Streaming API doesn't use DiskFileItemFactory. But it does use a buffer for copying data as BalusC has posted. Once you have the stream of the upload, you can use

long bytesCopied = Streams.copy(yourInputStream, yourOutputStream, true);  

Look at the API

like image 182
rickz Avatar answered Oct 01 '22 00:10

rickz


Here is the javadoc for DiskFileItemFactory.

The default FileItemFactory implementation. This implementation creates FileItem instances which keep their content either in memory, for smaller items, or in a temporary file on disk, for larger items. The size threshold, above which content will be stored on disk, is configurable, as is the directory in which temporary files will be created.

If not otherwise configured, the default configuration values are as follows:

Size threshold is 10KB.
Repository is the system default temp directory, as returned by System.getProperty("java.io.tmpdir").

Temporary files, which are created for file items, should be deleted later on. The best way to do this is using a FileCleaningTracker, which you can set on the DiskFileItemFactory. However, if you do use such a tracker, then you must consider the following: Temporary files are automatically deleted as soon as they are no longer needed. (More precisely, when the corresponding instance of File is garbage collected.) This is done by the so-called reaper thread, which is started automatically when the class FileCleaner is loaded. It might make sense to terminate that thread, for example, if your web application ends. See the section on "Resource cleanup" in the users guide of commons-fileupload.

So, yes close and cleanup are necessary, as FileItem may denote a real file on disk.

like image 43
Alexander Pogrebnyak Avatar answered Sep 30 '22 22:09

Alexander Pogrebnyak