Apache Jakarta Project: commons FileUpload用戶指南

最近一段時間都在爲文件上傳這件事焦心,總是找不合適的代碼,自己水平差寫不出什麼好東西出來,所以問題一直拖到現在。今天在google上看到commons FileUpload的相關簡介,馬上就去Apache Jakarta Project去找。哈哈……沒錯,就是它啦~
花了一個小時把它的E文Overview看了一下,覺得使用起來實在太方便了,短短地幾句話就可以完成上傳操作,真不賴,讓我又可以小偷懶一把:P。下午,又寫了一個簡單的Bean來測試裏面關鍵的類的使用,更具體的瞭解了它的方法作用的結果。再把那個E文User Guide主要的地方,在這裏翻譯一下。對於做頁面上傳文件的工作應該有一定的幫助。

Using FileUpload

使用FileUpload

FileUpload can be used in a number of different ways, depending upon the requirements of your application. In the simplest case, you will call a single method to parse the servlet request, and then process the list of items as they apply to your application. At the other end of the scale, you might decide to customize FileUpload to take full control of the way in which individual items are stored; for example, you might decide to stream the content into a database.
FileUpload可以根據應用程序的需求在很多不同的地方使用。舉個很簡單的例子,你可能調用一個簡單的方法去編譯servlet請求,並且把這些項目作爲你的應用程序一部分來應用。從另一個方面來講,你可能想自定義FileUpload來完成所有項目的存儲;再來個例子,你可能想流化內容而存入數據庫。

Here, we will describe the basic principles of FileUpload, and illustrate some of the simpler - and most common - usage patterns. Customization of FileUpload is described elsewhere .
這裏我們會介紹FileUpload基礎的使用原則,並描述一些簡單的通用的使用模式。關於FileUpload自定義的介紹會在其它地方(其實那裏什麼也沒有)講。

How it works

簡介

A file upload request comprises an ordered list of items that are encoded according to RFC 1867 , "Form-based File Upload in HTML". FileUpload can parse such a request and provide your application with a list of the individual uploaded items. Each such item implements t he FileItem inte***ce, regardless of its underlying implementation.
一個上傳請求由一系列根據RFC1867(這個文章我已經放在BLOG收藏夾HTML目錄下)編碼的項目。FileUpload可以編譯這樣的請求並將這一系列的個性化上傳項目傳遞給你的應用程序。每一個這樣的項目都實現了FielItem接口,不管它是怎麼實現的。

Each file item has a number of properties that might be of interest for your application. For example, every item has a name and a content type, and can provide an InputStream to access its data. On the other hand, you may need to process items differently, depending upon whether the item is a regular form field - that is, the data came from an ordinary text box or similar HTML field - or an uploaded file. The FileItem inte***ce provides the methods to make such a determination, and to access the data in the most appropriate manner.
每一個文件項目有一些自己的屬性,這些屬性也許正是你的應用程序感興趣的地方。例如,每個項目有個一個名字和內容類型,並且可以提供一個輸入流來訪問這寫數據。另一方面來看,你可能需要用不同方式來處理不同的項目,這就依賴與項目是否是一個正常的字域,也就是說,這些數據來自於一個普通的文本框或類似HTML的字域或一個上傳文件。FileItem接口提供一些方法來做這樣一個決定,並且訪問這些數據用最合適的方法。

FileUpload creates new file items using a FileItemFactory . This is what gives FileUpload most of its flexibility. The factory has ultimate control over how each item is created. The default factory stores the item′s data in memory or on disk, depending on the size of the item (i.e. bytes of data). However, this behavior can be customized to suit your application.
FileUpload使用FileItemFactory創建一個新的文件項目。對於FileUpload來說這是完全可行的。工廠是唯一全盤控制每個項目的創建的工具。默認的工廠存儲項目的數據在內存或者硬盤裏,這都依賴於項目的大小(如,數據字節數組)。不過,還是可以把它定義成爲合適你的應用程序使用的。

Parsing the request

Before you can work with the uploaded items, of course, you need to parse the request itself. Ensuring that the request is actually a file upload request is straightforward, but FileUpload makes it simplicity itself, by providing a static method to do just that.

在使用上傳項目工作之前,你還需要編譯請求。最重要的事情就要確定這個請求確實來自於文件上傳,不過FileUpload利用一個靜態方法使它自身簡單化了。

// Check that we have a file upload request

boolean isMultipart = FileUpload.isMultipartContent(request);

Now we are ready to parse the request into its constituent items.

現在我們可以準備開始編譯請求了。

The simplest case

簡單的例子

The simplest usage scenario is the following:

Uploaded items should be retained in memory as long as they are reasonably small.
Larger items should be written to a temporary file on disk.
Very large upload requests should not be permitted.
The built-in defaults for the maximum size of an item to be retained in memory, the maximum permitted size of an upload request, and the location of temporary files are acceptable.
下面是一些簡單的使用場景:

l        上傳項目只要足夠小,就應該保留在內存裏。

l        較大的項目應該被寫在硬盤的臨時文件上。

l        非常大的上傳請求應該避免。

l        限制項目在內存中所佔的空間,限制最大的上傳請求,並且設定臨時文件的位置。

Handling a request in this scenario couldn′t be much simpler:

處理這個場景的請求很簡單:

// Create a new file upload handler

DiskFileUpload upload = new DiskFileUpload();



// Parse the request

List /* FileItem */ items = upload.parseRequest(request);

That′s all that′s needed. Really!

這就是我們所需要的全部代碼了!

The result of the parse is a List of file items, each of which implements the FileItem inte***ce. Processing these items is discussed below.

編譯的結果就是生成了一系列文件項目,每個文件項目實現一個FileItem接口。下面將介紹如何處理這寫項目。

Exercising more control

控制練習

If your usage scenario is close to the simplest case, described above, but you need a little more control over the size thresholds or the location of temporary files, you can customize the behavior using the methods of the DiskFileUpload class, like this:

如果你的使用場景和最簡單例子很接近,但是你又需要一點點擴展的控制,包括大小的極限或者臨時文件的設置等,你可以通過DiskFileUpload類的方法來自定義行爲,像這樣:

// Create a new file upload handler

DiskFileUpload upload = new DiskFileUpload();



// Set upload parameters

upload.setSizeThreshold(yourMaxMemorySize);

upload.setSizeMax(yourMaxRequestSize);

upload.setRepositoryPath(yourTempDirectory);



// Parse the request

List /* FileItem */ items = upload.parseRequest(request);

Of course, each of the configuration methods is independent of the others, but if you want to configure them all at once, you can do that with an alternate parseRequest() method, like this:

當然,每個配置方法是獨立於其它任意一個的,但是如果你想一次性配置他們,你可以用parseRequest()的另一個重載方法,像這樣:

最近一段時間都在爲文件上傳這件事焦心,總是找不合適的代碼,自己水平差寫不出什麼好東西出來,所以問題一直拖到現在。今天在google上看到commons FileUpload的相關簡介,馬上就去Apache Jakarta Project去找。哈哈……沒錯,就是它啦~
花了一個小時把它的E文Overview看了一下,覺得使用起來實在太方便了,短短地幾句話就可以完成上傳操作,真不賴,讓我又可以小偷懶一把:P。下午,又寫了一個簡單的Bean來測試裏面關鍵的類的使用,更具體的瞭解了它的方法作用的結果。再把那個E文User Guide主要的地方,在這裏翻譯一下。對於做頁面上傳文件的工作應該有一定的幫助。

Using FileUpload

使用FileUpload

FileUpload can be used in a number of different ways, depending upon the requirements of your application. In the simplest case, you will call a single method to parse the servlet request, and then process the list of items as they apply to your application. At the other end of the scale, you might decide to customize FileUpload to take full control of the way in which individual items are stored; for example, you might decide to stream the content into a database.
FileUpload可以根據應用程序的需求在很多不同的地方使用。舉個很簡單的例子,你可能調用一個簡單的方法去編譯servlet請求,並且把這些項目作爲你的應用程序一部分來應用。從另一個方面來講,你可能想自定義FileUpload來完成所有項目的存儲;再來個例子,你可能想流化內容而存入數據庫。

Here, we will describe the basic principles of FileUpload, and illustrate some of the simpler - and most common - usage patterns. Customization of FileUpload is described elsewhere .
這裏我們會介紹FileUpload基礎的使用原則,並描述一些簡單的通用的使用模式。關於FileUpload自定義的介紹會在其它地方(其實那裏什麼也沒有)講。

How it works

簡介

A file upload request comprises an ordered list of items that are encoded according to RFC 1867 , "Form-based File Upload in HTML". FileUpload can parse such a request and provide your application with a list of the individual uploaded items. Each such item implements t he FileItem inte***ce, regardless of its underlying implementation.
一個上傳請求由一系列根據RFC1867(這個文章我已經放在BLOG收藏夾HTML目錄下)編碼的項目。FileUpload可以編譯這樣的請求並將這一系列的個性化上傳項目傳遞給你的應用程序。每一個這樣的項目都實現了FielItem接口,不管它是怎麼實現的。

Each file item has a number of properties that might be of interest for your application. For example, every item has a name and a content type, and can provide an InputStream to access its data. On the other hand, you may need to process items differently, depending upon whether the item is a regular form field - that is, the data came from an ordinary text box or similar HTML field - or an uploaded file. The FileItem inte***ce provides the methods to make such a determination, and to access the data in the most appropriate manner.
每一個文件項目有一些自己的屬性,這些屬性也許正是你的應用程序感興趣的地方。例如,每個項目有個一個名字和內容類型,並且可以提供一個輸入流來訪問這寫數據。另一方面來看,你可能需要用不同方式來處理不同的項目,這就依賴與項目是否是一個正常的字域,也就是說,這些數據來自於一個普通的文本框或類似HTML的字域或一個上傳文件。FileItem接口提供一些方法來做這樣一個決定,並且訪問這些數據用最合適的方法。

FileUpload creates new file items using a FileItemFactory . This is what gives FileUpload most of its flexibility. The factory has ultimate control over how each item is created. The default factory stores the item′s data in memory or on disk, depending on the size of the item (i.e. bytes of data). However, this behavior can be customized to suit your application.
FileUpload使用FileItemFactory創建一個新的文件項目。對於FileUpload來說這是完全可行的。工廠是唯一全盤控制每個項目的創建的工具。默認的工廠存儲項目的數據在內存或者硬盤裏,這都依賴於項目的大小(如,數據字節數組)。不過,還是可以把它定義成爲合適你的應用程序使用的。

Parsing the request

Before you can work with the uploaded items, of course, you need to parse the request itself. Ensuring that the request is actually a file upload request is straightforward, but FileUpload makes it simplicity itself, by providing a static method to do just that.

在使用上傳項目工作之前,你還需要編譯請求。最重要的事情就要確定這個請求確實來自於文件上傳,不過FileUpload利用一個靜態方法使它自身簡單化了。

// Check that we have a file upload request

boolean isMultipart = FileUpload.isMultipartContent(request);

Now we are ready to parse the request into its constituent items.

現在我們可以準備開始編譯請求了。

The simplest case

簡單的例子

The simplest usage scenario is the following:

Uploaded items should be retained in memory as long as they are reasonably small.
Larger items should be written to a temporary file on disk.
Very large upload requests should not be permitted.
The built-in defaults for the maximum size of an item to be retained in memory, the maximum permitted size of an upload request, and the location of temporary files are acceptable.
下面是一些簡單的使用場景:

l        上傳項目只要足夠小,就應該保留在內存裏。

l        較大的項目應該被寫在硬盤的臨時文件上。

l        非常大的上傳請求應該避免。

l        限制項目在內存中所佔的空間,限制最大的上傳請求,並且設定臨時文件的位置。

Handling a request in this scenario couldn′t be much simpler:

處理這個場景的請求很簡單:

// Create a new file upload handler

DiskFileUpload upload = new DiskFileUpload();



// Parse the request

List /* FileItem */ items = upload.parseRequest(request);

That′s all that′s needed. Really!

這就是我們所需要的全部代碼了!

The result of the parse is a List of file items, each of which implements the FileItem inte***ce. Processing these items is discussed below.

編譯的結果就是生成了一系列文件項目,每個文件項目實現一個FileItem接口。下面將介紹如何處理這寫項目。

Exercising more control

控制練習

If your usage scenario is close to the simplest case, described above, but you need a little more control over the size thresholds or the location of temporary files, you can customize the behavior using the methods of the DiskFileUpload class, like this:

如果你的使用場景和最簡單例子很接近,但是你又需要一點點擴展的控制,包括大小的極限或者臨時文件的設置等,你可以通過DiskFileUpload類的方法來自定義行爲,像這樣:

// Create a new file upload handler

DiskFileUpload upload = new DiskFileUpload();



// Set upload parameters

upload.setSizeThreshold(yourMaxMemorySize);

upload.setSizeMax(yourMaxRequestSize);

upload.setRepositoryPath(yourTempDirectory);



// Parse the request

List /* FileItem */ items = upload.parseRequest(request);

Of course, each of the configuration methods is independent of the others, but if you want to configure them all at once, you can do that with an alternate parseRequest() method, like this:

當然,每個配置方法是獨立於其它任意一個的,但是如果你想一次性配置他們,你可以用parseRequest()的另一個重載方法,像這樣

發佈了5 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章