-1
我在互聯網上搜索它說DiskItemFilefactory創建一個工廠的字符串文件和ServletFileUpload是一個文件處理程序。但我看到我們使用它們兩個設置文件的最大大小上傳。
請給一個邏輯實例來證明他們的工作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://stackoverflow.com/q/2422468 – BalusC