一、Commons-FileUpload简介
Commons-FileUpload组件
Commons是Apache开放源代码组织的一个Java子项目,其中的FileUpload是用来处理HTTP文件上传的子项目
Commons-FileUpload组件特点
使用简单:可以方便地嵌入到JSP文件中,编写少量代码即可完成文件的上传功能 能够全程控制上传内容 能够对上传文件的大小、类型进行控制
获取Commons-FileUpload组件的方式
http://commons.apache.org/fileupload下载Commons-FileUpload组件
Commons-FileUpload组件类库:commons-fileupload-1.2.2.jar
Commons-FileUpload组件的API文档: apidocs
http://commons.apache.org/io下载Commons-IO组件 Commons-IO组件
类库:commons-io-2.4.jar
Commons-IO组件的API文档: commons-io-2.4\docs
二、Commons-FileUpload组件的API
三、Commons-FileUpload组件应用
四、代码
index.jsp
String basePath= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
上传选择图片:
DoUpLoadServlet
packagecom.qj.servlet;importjava.io.File;importjava.io.IOException;importjava.io.PrintWriter;importjava.util.Arrays;importjava.util.Iterator;importjava.util.List;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.apache.commons.fileupload.FileItem;importorg.apache.commons.fileupload.FileUploadBase;importorg.apache.commons.fileupload.disk.DiskFileItemFactory;importorg.apache.commons.fileupload.servlet.ServletFileUpload;/*** Servlet implementation class DoUpLoadServlet*/@WebServlet("/DoUpLoadServlet")public class DoUpLoadServlet extendsHttpServlet {private static final long serialVersionUID = 1L;/***@seeHttpServlet#HttpServlet()*/
publicDoUpLoadServlet() {super();//TODO Auto-generated constructor stub
}/***@seeHttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
request.setCharacterEncoding("UTF-8");
String uploadFileName= ""; //上传的文件名
String fieldName = ""; //表单字段元素的name属性值
PrintWriter out = response.getWriter();//获取out对象//请求信息中的内容是否是multipart类型
boolean isMultipart =ServletFileUpload.isMultipartContent(request);//上传文件的存储路径(服务器文件系统上的绝对文件路径)
String uploadFilePath = request.getSession().getServletContext().getRealPath("upload/");//创建临时文件目录路径
File tempPatchFile=new File("d:\\temp\\buffer\\");if(!tempPatchFile.exists()) //判断文件或目录是否存在
tempPatchFile.mkdirs(); //创建指定的目录,包括所有必需但不存在的父目录
if(isMultipart) {
DiskFileItemFactory factory=newDiskFileItemFactory();//设置缓冲区大小4kb
factory.setSizeThreshold(4096);//设置上传文件用到临时文件存放路径
factory.setRepository(tempPatchFile);
ServletFileUpload upload= newServletFileUpload(factory);//设置单个文件的最大限制
upload.setSizeMax(1024*30);try{//解析form表单中所有文件
List items =upload.parseRequest(request);
Iterator iter =items.iterator();while (iter.hasNext()) { //依次处理每个文件
FileItem item =(FileItem) iter.next();if (!item.isFormField()){ //文件表单字段
String fileName =item.getName();//通过Arrays类的asList()方法创建固定长度的集合
List filType=Arrays.asList("gif","bmp","jpg");
String ext=fileName.substring(fileName.lastIndexOf(".")+1);if(!filType.contains(ext)) //判断文件类型是否在允许范围内
out.print("上传失败,文件类型只能是gif、bmp、jpg");else{if (fileName != null && !fileName.equals("")) {
File fullFile= newFile(item.getName());
File saveFile= newFile(uploadFilePath, fullFile.getName());
item.write(saveFile);
uploadFileName=fullFile.getName();
out.print("上传成功后的文件名是:"+uploadFileName+
",文件大小是:"+item.getSize()+"bytes!");
}
}
}
}
}catch(FileUploadBase.SizeLimitExceededException ex){
out.print("上传失败,文件太大,单个文件的最大限制是:"+upload.getSizeMax()+"bytes!");
}catch(Exception e) {
e.printStackTrace();
}
}
}/***@seeHttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
doGet(request, response);
}
}