FileUpload 是 Apache commons下面的一个子项目,用来实现Java环境下面的文件上传功能,
commons- fileupload-1.2.1:http://l7.yunpan.cn/lk/Qv7QNNd7KJjFM
依赖于
commons-io-1.4:http://l7.yunpan.cn/lk/Qv7Qpp3qapF9A
demo参考
package com.sunflower.servlet;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class FileUpload extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
//Post方法
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码
req.setCharacterEncoding("UTF-8");
//文件上传处理
fileControl(req, resp);
}
/**
* 上传文件的处理
*/
private void fileControl(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
// 在解析请求之前先判断请求类型是否为文件上传类型
// boolean isMultipart = ServletFileUpload.isMultipartContent(req);
// 文件上传处理工厂
FileItemFactory factory = new DiskFileItemFactory();
//创建临时文件目录
// File tempFile = new File(req.getRealPath("/temp"));
File tempFile = new File(this.getServletContext().getRealPath("/temp"));
//设置缓存大小
((DiskFileItemFactory) factory).setSizeThreshold(1024*1024);
//设置临时文件存放地点
((DiskFileItemFactory) factory).setRepository(tempFile);
// 创建文件上传处理器
ServletFileUpload upload = new ServletFileUpload(factory);
//将页面请求传递信息最大值设置为50M
upload.setSizeMax(1024*1024*50);
//将单个上传文件信息最大值设置为6M
upload.setFileSizeMax(1024*1024*12);
// 开始解析请求信息
List items = null;
try {
items = upload.parseRequest(req);
}
catch (FileUploadException e) {
e.printStackTrace();
}
// 对所有请求信息进行判断
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
// 信息为普通的格式
if (item.isFormField()) {
String fieldName = item.getFieldName();
String value = item.getString();
System.out.println("formfile文件名为:"+fieldName+"值:"+value);
req.setAttribute(fieldName, value);
}
// 信息为文件格式
else {
long d =item.getSize();
if(d>upload.getSizeMax()){
try {
resp.sendRedirect("/index");
} catch (IOException e) {
e.printStackTrace();
}
}
String fileName = item.getName();
System.out.println("upload_file文件名为:"+fileName);
int index = fileName.lastIndexOf("\\");
fileName = fileName.substring(index+1 );
System.out.println("upload_file文件名为:"+fileName);
System.out.println("文件大小"+d);
req.setAttribute("realFileName", fileName);
//写进文件
String basePath = this.getServletContext().getRealPath("/uploadFile");
System.out.println("basePath is : "+basePath);
File file = new File(basePath, fileName);
try {
item.write(file);
System.out.println("写入成功!");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
// try {
// req.getRequestDispatcher("/download.jsp").forward(req, resp);
// }
// catch (IOException e) {
// e.printStackTrace();
// }
}
}