2008年1月10日木曜日

[Java>Apache File Uploader]Sample Program how to use Apache File Uploder

Sample Program how to use Apache File Uploder

References
  • Apache FileUploader
    http://commons.apache.org/fileupload/
  • JavaDoc
    http://commons.apache.org/fileupload/apidocs/index.html
Install and Setup
  1. Get latest jar file of Apache file uploader
    http://commons.apache.org/downloads/download_fileupload.cgi
  2. Get dependency library files
    http://commons.apache.org/fileupload/dependencies.html
  3. Put these files into the program library directory
Sample of HTML file to upload any file from the client's browser
Following sample program has 2 form parameters, one is a file type input parameter, other is a normal text parameter.We can get these 2 types parameter values in the same form.
The form part in HTML is needed the value "enctype="multipart/form-data" in form tag, and input type of the field for file upload has to be set to "file".
Following is a sample html of form part. The value of the action parameter in form tag is a sample method name related to the sample servlet program described below.
<form enctype="multipart/form-data" action="execFileUpload&mode=imageUpload" method="post">
<input size="60" type="file" name="fileName1" /><br>
<input type="text" name="comment" value="" /><br>
</textarea>

<input id="submit" name="submit" type="submit" value="Upload" />
</form>
Import Classes
//Apache FileUploader Classes
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import java.util.Iterator;

//Image Utility
import javax.imageio.ImageIO;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
Sample Program
public Template execFileUpload(HttpServletRequest request,HttpServletResponse response)
{
/*
Check whether the received HttpServletRequest object is multipart or not.
Normally, the HttpServletRequest of servlet program is not multipart.
If the HttpServletRequest is not multipart, the request doesn't include file data.
Check whether the request is multipart or not by using the ServletFileUpload.isMultipartContent.
*/
boolean isMultipart=ServletFileUpload.isMultipartContent(request);
if(isMultipart){
/*
If the request is multipart, get file data
Initialize FileItemFactory and ServletFileUpload objects
*/
FileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload upload=new ServletFileUpload(factory);

/*
Get the List object including the file and other values of form parameters,
and then create and get List's iterator object.
We can only use this Iterator object for Multipart List object once.
*/
List items=upload.parseRequest(request);
Iterator itemsIterator=items.iterator();

/*
How to get form parameters.
*/
/*
We can get the value of parameters included into the action method which is not
input parameter. This is a normal parameter.
In this case above, action method in form tag is including the "mode" parameter.
This parameter which is not a form's input parameter can be get the value by using the
HttpServletRequest.getParameter() method.
*/
String mode=request.getParameter("mode");

/*
Get item from the list
*/
while(itemsIterator.hasNext()){
/*
Get parameters in form tags, input text, hidden, submit parameters.
We can get any input parameters as the FileItem object.
*/
FileItem item=(FileItem)itemsIterator.next();

/*
Get the value of ContentType.
If the input type is defined as "file", the value of ContentType is set to the
Image type like "image/jpeg".
If the intput type is not "file", the value of ContentType is set to null.
*/
String itemContentType=item.getContentType();

/*
Get the value of the file name
If the input type is defined as "file", the getName() method of FileItem object
returns the file name specified by the client when uploading the file.
If the intput type is not "file", the value of getName() is set to null.
*/
String itemFileName=item.getName();

/*
Get the value of intput attribute name.
*/
String itemFieldName=item.getFieldName();

/*
Get and check the form's field type.
If the input type is defined as "file", the isFormField() method of FileItem object
returns false.
If the intput type is not "file", the value of isFormField() returns true.
*/
if(item.isFormField()){
/*
Get the value of normal input parameter.
If the intput type is not "file", get the value by using FileItem's getString() method.
*/
String itemFieldValue=item.getString();
}
else{
/*
Get and check the file's field name.
We defines the name of "fileName1" for Intput form attribute.
*/
if(itemFieldName!=null && itemFieldName.equals("fileName1")){
/*
Get byte arrays for the file sent from the client.
*/
byte[] itemBytes=item.get();
/*
Get InputStream object
*/
InputStream itemInputStream=item.getInputStream();

/*
Convert InputStream object to BufferedImage object if you need.
In this case, put the image file to the disk storage.
*/
BufferedImage bufferedImage=ImageIO.read(itemInputStream);
/*
Create File object and output BufferedImage object to the File.
*/
File imageFile=new File("/tmp/test.jpg");
boolean outResult=ImageIO.write(bufferedImage,"jpg",imageFile);
......
}
}
}
}
}

0 件のコメント: