1、环境 : JDK 1.6 , Tomcat 7.0
2、第三方类库:
commons-fileupload-1.3.1.jar
commons-io-2.4.jar
3、web.xml配置:
FileUpload java02.rhythmk.com.FileUploadServlet filepath uploadfile temppath temp FileUpload /FileUpload.html
4、 jsp :
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>Insert title here
5、Servlet:
package java02.rhythmk.com;import java.io.File;import java.io.IOException; import java.util.List; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; 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; /** * Servlet implementation class FileUploadServlet */ @WebServlet("/FileUploadServlet") public class FileUploadServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static String filepath = ""; private static String temp = ""; /** * Default constructor. */ public FileUploadServlet() { // TODO Auto-generated constructor stub } @Override public void init(ServletConfig config) throws ServletException { // TODO Auto-generated method stub super.init(config); if (filepath == "" || temp == "") { filepath = config.getInitParameter("filepath"); temp = config.getInitParameter("temppath"); ServletContext context = getServletContext(); filepath = context.getRealPath(filepath); temp = context.getRealPath(temp); System.out.println("filepath:" + filepath + ",temp:" + temp); } } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); try { DiskFileItemFactory foctory = new DiskFileItemFactory(); // 设置临时文件 foctory.setRepository(new File(temp)); // 设置临时文件缓存区大小 foctory.setSizeThreshold(1024 * 1024); ServletFileUpload sFileUpload = new ServletFileUpload(foctory); Listlist = (List ) sFileUpload .parseRequest(request); for (FileItem item : list) { String name = item.getFieldName(); if (item.isFormField()) { System.out.println("filedName:" + name + ",value:" + item.getString()); } else { String filename = item.getName(); System.out.println("value:" + filename); if (filename.length() > 0) { System.out.println("文件保存路径:" + filepath + "\\" + filename); File file = new File(filepath); if (!file.exists()) { file.mkdir(); } // 保存文件 item.write(new File(filepath, filename)); response.getWriter().write( "文件保存路径:" + filepath + "\\" + filename); } } } } catch (Exception e) { e.printStackTrace(); } } }