I have searched on internet it says the DiskItemFilefactory creates a Factory for string the files and the ServletFileUpload is a file handler.But i saw we use both of them for setting max size of file to be Uploaded.
Please give a logical instance to demonstrate their working

- 13
- 7
-
Just in case you didn't know yet: Apache Commons FileUpload is unnecessary since Servlet 3.0 (December 2009) comes with its own API. See also http://stackoverflow.com/q/2422468 – BalusC Feb 13 '16 at 12:28
1 Answers
ServletFileUpload
is a file upload handler. How the data for individual parts is stored is determined by the factory used to create them; a given part may be in memory, on disk, or somewhere else.
If you look at the source code of the ServletFileUpload
class, you will see:
// ----------------------------------------------------------- Constructors
/**
* Constructs an uninitialised instance of this class. A factory must be
* configured, using <code>setFileItemFactory()</code>, before attempting
* to parse requests.
*
* @see FileUpload#FileUpload(FileItemFactory)
*/
public ServletFileUpload() {
super();
}
/**
* Constructs an instance of this class which uses the supplied factory to
* create <code>FileItem</code> instances.
*
* @see FileUpload#FileUpload()
* @param fileItemFactory The factory to use for creating file items.
*/
public ServletFileUpload(FileItemFactory fileItemFactory) {
super(fileItemFactory);
}
DiskFileItemFactory
is 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.
The simplest case from Using FileUpload:
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Configure a repository (to ensure a secure temp location is used)
ServletContext servletContext = this.getServletConfig().getServletContext();
File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
factory.setRepository(repository);
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
List<FileItem> items = upload.parseRequest(request);
And, of course, you mean DiskFileItemFactory
but not the DiskItemFilefactory
.