fileUpload解析
此日志源于http://blog.163.com/rqfreedom@126/
一:使用fileUpload时,我们首先要将commons-io.jar和commons-fileupload-1.2.1.jar两个文件放在所在项目的webroot/bin, 从新发布项目
二:java代码
package servlet;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
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.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UpFileProcessServlet extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
request.setCharacterEncoding("UTF-8");
this.FileUpCenter(request, response);
}
private void FileUpCenter(HttpServletRequest request, HttpServletResponse response){
//处理文件上传 先创建ServletFileUpload
ServletFileUpload upload = new ServletFileUpload();
//设置整个请求最大值
upload.setSizeMax(-1);
//设置单个文件最大值
upload.setFileSizeMax(1024*1024*10);
//创建文件项目工厂
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
//设置内存缓存 默认为1024*5
diskFileItemFactory.setSizeThreshold(1024*5);
File file = new File(this.getServletContext().getRealPath("/upload/temp"));
if(!file.exists()){
file.mkdirs();
}
//设置超过缓存的文件存储目录
diskFileItemFactory.setRepository(file);
//把该工厂设置到upload 使其具有以上属性
upload.setFileItemFactory(diskFileItemFactory);
//解析请求中的数据 创建FILEITEM
List<FileItem> fileItems=null;
try {
fileItems=upload.parseRequest(request);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//处理每个表单的单个字段 创建迭代器
Iterator i =fileItems.iterator();
//迭代取出每个fileItem 进行分析
while (i.hasNext()) {
FileItem fileItem = (FileItem) i.next();
if(!fileItem.isFormField()){
//通过getName()方法获得表单中name对应的文件名
//只要表单中存在name名 即使不上传任何文件 也会存在一个fileItem 应进行非空判断
String pathThatSrc=fileItem.getName();
if(pathThatSrc.trim().equals("")){
continue;
}
//获取文件后缀
String prefix=pathThatSrc.substring(pathThatSrc.lastIndexOf('.'));
//通过UUID 获得一唯一文件名
String fileName=UUID.randomUUID().toString()+prefix;
//通过文件和目录创建file对象
File buildFile = new File(this.getServletContext().getRealPath("/upload/filedir"));
if(!buildFile.exists()){
buildFile.mkdirs();
}
try {
File filePathFile= new File(buildFile,fileName);
fileItem.write(filePathFile);
String fileNamePath=filePathFile.getPath();
System.out.println("fadsfadsfasd"+fileNamePath);
request.setAttribute("path",fileName);
request.getRequestDispatcher("Test.jsp").forward(request,response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
fileItem.delete();
}
}
}
}
}