|
|||||||||
| 前のパッケージ 次のパッケージ | フレームあり フレームなし | ||||||||
参照:
説明
| インタフェースの概要 | |
| FileItem | このクラスはクライアントから受け取る multipart/form-data
のPOSTリクエスト内のファイルまたはアイテムを表現したものです。
|
| FileItemFactory | FileItem インスタンスを生成するためのファクトリインターフェイスです。
|
| クラスの概要 | |
| DefaultFileItem | FileItem
インターフェイスの標準実装です。
|
| DefaultFileItemFactory | FileItemFactory インターフェイスの標準実装です。
|
| DeferredFileOutputStream | 指定された閾値に達するまではデータをメモリ上に保持し、 閾値に達した場合にはディスク上に出力する出力ストリームです。 |
| DiskFileUpload | ファイルアップロードを処理するための高レベルAPIです。 |
| FileUpload | ファイルアップロードを処理するための高レベルAPIです。 |
| FileUploadBase | ファイルアップロードを処理するための高レベルAPIです。 |
| MultipartStream | ファイルアップロードを処理するための低レベルAPIです。 |
| ThresholdingOutputStream | 指定されたバイト数のデータが書き込まれた時にイベントを発生させる出力ストリームです。 |
| 例外の概要 | |
| FileUploadBase.InvalidContentTypeException | リクエストが multipart リクエストでない場合に投げられます。 |
| FileUploadBase.SizeLimitExceededException | リクエストのサイズが設定された最大サイズを超えた場合に投げられます。 |
| FileUploadBase.UnknownSizeException | リクエストのサイズが明示されていない場合に投げられます。 |
| FileUploadException | リクエストを処理する際にエラーが発生した事を示す例外です。 |
html ファイルアップロードの扱いに関する拡張は RFC 1867 によって規定されています。 RFC 1867
一般的なこのパッケージの使用方法は、DiskFileUpload
により HttpServletRequest を解析し、FileItem
のリストを取得するというものです。
この FileItem を使うとアップロードされたデータに簡単にアクセスできます。
また、このパッケージにはアップロードデータを MultipartStream
クラスにカプセル化し、手動で操作するための低レベルAPIもあります。
FileItem's provide easy access to the data
given in the upload. There is also a low level api for
manipulating the upload data encapsulated in the
{@link org.apache.commons.fileupload.MultipartStream MultipartStream}
class.一般的な使用方法の例:
public void doPost(HttpServletRequest req, HttpServletResponse res)
{
DiskFileUpload fu = new DiskFileUpload();
// (データの)最大サイズで、これを超えると FileUploadException が投げられる
fu.setSizeMax(1000000);
// メモリに保持できる(データの)最大サイズ
fu.setSizeThreshold(4096);
// getSizeThreshold() の値より大きなデータを保存する場所
fu.setRepositoryPath("/tmp");
List fileItems = fu.parseRequest(req);
// 2つのファイルがある(アップロードされる)ことが分かっていると仮定
// 1つ目のファイルは小さなテキストファイル、
// 2つ目のファイルの詳細は不明なのでサーバ上のファイルに書き出す
Iterator i = fileItems.iterator();
String comment = ((FileItem)i.next()).getString();
FileItem fi = (FileItem)i.next();
// クライアントでのファイル名
String fileName = fi.getName();
// コメントとファイル名をデータベースに保存
...
// ファイルに書き出し
fi.write("/www/uploads/" + fileName);
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
{
DiskFileUpload fu = new DiskFileUpload();
// maximum size before a FileUploadException will be thrown
fu.setSizeMax(1000000);
// maximum size that will be stored in memory
fu.setSizeThreshold(4096);
// the location for saving data that is larger than getSizeThreshold()
fu.setRepositoryPath("/tmp");
List fileItems = fu.parseRequest(req);
// assume we know there are two files. The first file is a small
// text file, the second is unknown and is written to a file on
// the server
Iterator i = fileItems.iterator();
String comment = ((FileItem)i.next()).getString();
FileItem fi = (FileItem)i.next();
// filename on the client
String fileName = fi.getName();
// save comment and filename to database
...
// write the file
fi.write("/www/uploads/" + fileName);
}
上記の例では最初のファイルは String としてメモリ上に展開されます。
getString メソッドがコールされるまで、データはサイズに応じてメモリまたはディスクに保持されています。
ここで想定している2番目のファイルは大きいため、明示的にメモリにロードされることはありませんが、
もし4096バイト未満なら(指定された)最終格納場所に書き出されるまでメモリに保持されます。
最終格納場所に書き出す際に、もしデータが閾値(threshold)より大きい場合には、
一時ファイルを指定された場所に移動する事を試みます。
それができなかった場合には、指定された場所に新たに書き出しを行います。
String. Before calling the getString method, the data
may have been in memory or on disk depending on its size. The second
file we assume it will be large and therefore never explicitly load
it into memory, though if it is less than 4096 bytes it will be
in memory before it is written to its final location. When writing to
the final location, if the data is larger than the
threshold, an attempt is made to rename the temporary file to
the given location. If it cannot be renamed, it is streamed to the
new location.
|
|||||||||
| 前のパッケージ 次のパッケージ | フレームあり フレームなし | ||||||||