部落客廣告聯播

2007年5月19日 星期六

筆記:使用Jakarta Upload元件(Note: Using Jakarta Upload component)

1. 記得 form method="post" enctype="multipart/form-data"
1. Remember
form method="post" enctype="multipart/form-data".

2. 參考http://jakarta.apache.org/commons/fileupload/
2. Refer to http://jakarta.apache.org/commons/fileupload/

3. Commons-IO為必要,同樣可從jakarta網站下載。
3. Commons-IO is required,which can be downloaded from the site of jakarta.

4. 基本使用步驟:
1. 檢察是否正確的multipart Request。
ServletFileUpload.isMultipartContent(request)
2.解析。
FileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload upload=new ServletFileUpload(factory);
List items=upload.parseRequest(request);
3.處理FileItem(可能為上傳後的檔案、或為表單欄位)
// process file items
Iterator it=items.iterator();
while(it.hasNext())
{
FileItem item=(FileItem)it.next();
String newFileName="";

if(item.isFormField())
{
out.println("Form field parsed:");
out.println("\t field contentType(getContentType()):"+item.getContentType()); // it will be null
out.println("\t field name(getFieldName()):"+item.getFieldName());
out.println("\t value(getString()):"+item.getString());
if(item.getFieldName().equals("fileName")) newFileName=item.getString();
}else
{
out.println("File field parsed:");
out.println("\t field contentType(getContentType()):"+item.getContentType());
out.println("\t field name(getFieldName()):"+item.getFieldName());
out.println("\t value(getString()):"+item.getString());
out.println("\t original file name:"+item.getName());
out.println("\t file size(getSize()):"+String.valueOf(item.getSize()));
out.println("\t isInMemory():"+String.valueOf(item.isInMemory()));
}

out.println("");

}
4.可用FileItem的write(File)寫入檔案,getInputStream()、getOutputStream()進行IO處理。


4.Basic Usage steps:
1. Check request is valid multipart content. (It's a static method)
ServletFileUpload.isMultipartContent(request)
2. parse
FileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload upload=new ServletFileUpload(factory);
List items=upload.parseRequest(request);
3. process FileItems(it maybe a uploaded file, or a form filed)
// process file items
Iterator it=items.iterator();
while(it.hasNext())
{
FileItem item=(FileItem)it.next();
String newFileName="";

if(item.isFormField())
{
out.println("Form field parsed:");
out.println("\t field contentType(getContentType()):"+item.getContentType()); // it will be null
out.println("\t field name(getFieldName()):"+item.getFieldName());
out.println("\t value(getString()):"+item.getString());
if(item.getFieldName().equals("fileName")) newFileName=item.getString();
}else
{
out.println("File field parsed:");
out.println("\t field contentType(getContentType()):"+item.getContentType());
out.println("\t field name(getFieldName()):"+item.getFieldName());
out.println("\t value(getString()):"+item.getString());
out.println("\t original file name:"+item.getName());
out.println("\t file size(getSize()):"+String.valueOf(item.getSize()));
out.println("\t isInMemory():"+String.valueOf(item.isInMemory()));
}

out.println("");

}
4. method wirte(File) on FileItem can be used to write uploaded file into disk, and getInputStream() and getOutputStream() can be called to do more IO process.

沒有留言: