DiskItemFileFactory和ServletFileUpload之间的逻辑区别是什么?
问题描述:
我在互联网上搜索它说DiskItemFilefactory创建一个工厂的字符串文件和ServletFileUpload是一个文件处理程序。但我看到我们使用它们两个设置文件的最大大小上传。
请给一个逻辑实例来证明他们的工作DiskItemFileFactory和ServletFileUpload之间的逻辑区别是什么?
答
ServletFileUpload
是一个文件上传处理程序。如何存储单个零件的数据由用于创建它们的工厂确定;给定的部分可能在内存中,磁盘上或其他地方。
如果你看一下ServletFileUpload
类的源代码,您将看到:
// ----------------------------------------------------------- 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
是默认FileItemFactory
实现。这个实现创建了FileItem
实例,它们将内容保存在内存中,对于较小的项目,或者对于较大的项目,将其内容保存在磁盘上的临时文件中。内容将存储在磁盘上的大小阈值是可配置的,就像在其中创建临时文件的目录一样。
最简单的情况下,从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);
,当然,你的意思是DiskFileItemFactory
但不是DiskItemFilefactory
。
以防万一您还不知道:Apache Commons FileUpload是不必要的,因为Servlet 3.0(2009年12月)带有自己的API。另见http://*.com/q/2422468 – BalusC