I was reading through the Apache FileUpload stuff and it looks really simple to use but I got a little confused by some of the wording of how certain things are supposed to be done.
For example,
+"With uploaded files, you generally will not want to access them via memory, unless they are small, or unless you have no other alternative. Rather, you will want to process the content as a stream, or write the entire file to its ultimate location. FileUpload provides simple means of accomplishing both of these.+
{code}// Process a file upload
if (writeToFile) {
File uploadedFile = new File(...);
item.write(uploadedFile);
} else {
InputStream uploadedStream = item.getInputStream();
...
uploadedStream.close();
}{code}
+Note that, in the default implementation of FileUpload, write() will attempt to rename the file to the specified destination, if the data is already in a temporary file. Actually copying the data is only done if the the rename fails, for some reason, or if the data was in memory.+
+If you do need to access the uploaded data in memory, you need simply call the get() method to obtain the data as an array of bytes."+
// Process a file upload in memory
byte[] data = item.get();
What is the difference between just having the data in memory and streaming it? If I'm streaming it, isn't it in memory? Where is it being streamed from if it's not on memory?
"+Resource cleanup+
+This section applies only, if you are using the DiskFileItem . In other words, it applies, if your uploaded files are written to temporary files before processing them.+
+Such temporary files are deleted automatically, if they are no longer used (more precisely, if the corresponding instance of java.io.File is garbage collected. This is done silently by the org.apache.commons.io.FileCleaner class, which starts a reaper thread.+
+This reaper thread should be stopped, if it is no longer needed. In a servlet environment, this is done by using a special servlet context listener, called FileCleanerCleanup . To do so, add a section like the following to your web.xml:+
{code}<web-app>
...
<listener>
<listener-class>
org.apache.commons.fileupload.servlet.FileCleanerCleanup
</listener-class>
</listener>
...
</web-app>{code}
+Creating a DiskFileItemFactory+
+The FileCleanerCleanup provides an instance of org.apache.commons.io.FileCleaningTracker. This instance must be used when creating a org.apache.commons.fileupload.disk.DiskFileItemFactory. This should be done by calling a method like the following:+
{code} public static DiskFileItemFactory newDiskFileItemFactory(ServletContext context,
File repository) {
FileCleaningTracker fileCleaningTracker
= FileCleanerCleanup.getFileCleaningTracker(context);
return new DiskFileItemFactory(fileCleaningTracker,
DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD,
repository);
}{code}
My understanding was that FileUpload automatically decided what it was going to save to disk and what it wasn't going to save to disk. If something is going to be saved to disk I would very much like for it to be a temporary file that is automatically deleted. How can I ensure that this happens? I imagine I call the newDiskFileItemFactory instead of just creating a normal one, and that will deal with cleaning up the temp files, but how do I actually make them?