这段代码是之前做项目时写的,怕以后用到,先贴上来保存一下
使用之前要先加载commons-fileupload-1.2.jar
/*
* @(#)UsersManagerBean
*
* Copyright 2008 School of Software, Yunnan University.
* All rights reserved
*/
package user;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* 文件上传
* @version 1.0.0.0 2008-5-18
* @author meteorWJ
*/
public class FileUploadServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
// 定义文件的上传路径
private String uploadPath = "E:\\upload\\";
// 限制文件的上传大小
private int maxPostSize = 100 * 1024 * 1024;
public FileUploadServlet() {
super();
}
public void destroy() {
super.destroy();
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("Access !");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//保存文件到服务器中
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(maxPostSize);
try {
List fileItems = upload.parseRequest(request);
Iterator iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
String name = item.getName();
System.out.println(name);
try {
item.write(new File(uploadPath + name));
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (FileUploadException e) {
e.printStackTrace();
System.out.println(e.getMessage() + "结束");
}
}
}
使用之前要先加载commons-fileupload-1.2.jar
/*
* @(#)UsersManagerBean
*
* Copyright 2008 School of Software, Yunnan University.
* All rights reserved
*/
package user;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* 文件上传
* @version 1.0.0.0 2008-5-18
* @author meteorWJ
*/
public class FileUploadServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
// 定义文件的上传路径
private String uploadPath = "E:\\upload\\";
// 限制文件的上传大小
private int maxPostSize = 100 * 1024 * 1024;
public FileUploadServlet() {
super();
}
public void destroy() {
super.destroy();
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("Access !");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//保存文件到服务器中
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(maxPostSize);
try {
List fileItems = upload.parseRequest(request);
Iterator iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
String name = item.getName();
System.out.println(name);
try {
item.write(new File(uploadPath + name));
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (FileUploadException e) {
e.printStackTrace();
System.out.println(e.getMessage() + "结束");
}
}
}