FileUpload.java服务器端源码: package com.xiangzi.upload; import java.io.File; import java.io.IOException; import java.util.Iterator; import java.util.List; import javax.servlet.ServletContext; 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 FileUpLoad extends HttpServlet { private static final long serialVersionUID = -4904902402984274063L; String realPath = ""; ServletContext sc = null; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); //创建工厂对象 DiskFileItemFactory factory = new DiskFileItemFactory(); //创建上传文件对象,参数传的是工厂对象 ServletFileUpload up = new ServletFileUpload(factory); //设置文件的大小 up.setFileSizeMax(1024*1024*50); try { //对request进行解析,返回一个List List<FileItem> items = up.parseRequest(request); System.out.println("111"); Iterator<FileItem> it = items.iterator(); while (it.hasNext()) { FileItem item = it.next(); if (item.isFormField()) {// 若是一个普通的表单 System.out.println(item.getFieldName() + " , " + item.getString("UTF-8")); } else { // 若是文件 if (item.getName() != null && item.getName() != "") { System.out.println("文件的大小为:" + item.getSize() + "文件的类型为:" + item.getContentType()); File tempFile = new File(item.getName()); File filePath = new File(sc.getRealPath("/") + realPath); if (!filePath.exists()) { filePath.mkdir(); } File file = new File(filePath,tempFile.getName()); System.out.println(file.getAbsolutePath()); //把文件写到指定的文件夹中 item.write(file); request.setAttribute("resultMessage", "上传成功"); }else{ request.setAttribute("resultMessage", "没有选择文件"); } } } } catch (Exception e) { request.setAttribute("resultMessage", "上传失败"); e.printStackTrace(); } request.getRequestDispatcher("/result.jsp").forward(request, response); } @Override public void init() throws ServletException { sc = this.getServletContext(); //获取web.xml文件中设置的配置 路径 realPath = this.getInitParameter("path"); } } jsp:文件: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>文件上传实例</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css"> --> </head> <body> <form method="post" action="servlet/FileUpLoad" enctype="multipart/form-data"> <table> <tbody> <tr><td>上传人:</td><<td><input type="text" name="uploadName"/></td></tr> <tr><td>上传文件</td><td><input type="file" name="ff" id="upload"/></td></tr> <tr><td><input type="reset" value="重置"/></td><td><input type="submit" value="提交" id="cc"/></td></tr> </tbody> </table> <div id="results"></div> </form> </body> </html>