页面html代码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>上传文件</title> </head> <body> <font color="red" size="3px"><b>${error}</b></font> <table border="1" width="60%"> <tr> <th>用户名</th> <th>性别</th> <th>头像</th> <th>IP地址</th> <th>上传文件名</th> <th>操作</th> </tr> <c:forEach items="${list}" var="user"> <tr> <td>${user.name}</td> <td>${user.sex==true?"男":"女"}</td> <td><img src="${user.pic}" height="120"></td> <td>${user.ip}</td> <td>${user.fileName}</td> <td><a href="/upload?mode=download&id=${user.id}">下载</a></td> </tr> </c:forEach> </table> <form action="/upload?mode=upload" method="post"> <input type="submit" value="去上传..."/> </form> </body> </html> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>上传文件</title> <script type="text/javascript"> var tag = true; function go(){ if(tag){ document.forms[0].submit(); tag = false; }else{ alert("正在提交中,请勿重复提交请求……"); } } </script> </head> <body> <font color="red" size="3px"><b>${error}</b></font> <form action="/upload?mode=add" method="post" enctype="multipart/form-data"> <input type="hidden" name="randomName" value="${randomName}"/> <input type="hidden" name="randomValue" value="${randomValue}"/> <table> <tr> <td>用户名:</td> <td><input type="text" name="name"/></td> </tr> <tr> <td>性 别:</td> <td><input type="radio" name="sex" value="true"/>男 <input type="radio" name="sex" value="false"/>女 </td> </tr> <tr> <td>头 像:</td> <td><input type="file" name="pic"/></td> </tr> <!-- <tr> <td>IP地址:</td> <td><input type="text" name="ip"/></td> </tr> --> <tr> <td>文件名:</td> <td><input type="file" name="fileName"/></td> </tr> <tr> <!-- <td colspan="2" align="center"><input type="button" value="提交" onclick="go()"/></td> --> <td colspan="2" align="center"><input type="submit" value="提交""/> </tr> </table> </form> </body> </html>
java代码:
package cn.itcast.cd.domain; public class User { private Long id; private String name; private Boolean sex; private String pic; private String ip; private String fileName; private String filePath; private long fileSize; public String getName() { return name; } public void setName(String name) { this.name = name; } public Boolean getSex() { return sex; } public void setSex(Boolean sex) { this.sex = sex; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getFilePath() { return filePath; } public void setFilePath(String filePath) { this.filePath = filePath; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public String getPic() { return pic; } public void setPic(String pic) { this.pic = pic; } public long getFileSize() { return fileSize; } public void setFileSize(long fileSize) { this.fileSize = fileSize; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", sex=" + sex + ", pic=" + pic + ", ip=" + ip + ", fileName=" + fileName + ", filePath=" + filePath + ", fileSize=" + fileSize + "]"; } } package cn.itcast.cd.dao; import java.util.List; import cn.itcast.cd.domain.User; public interface IUserDAO { void add(User user); List<User> list(); User get(Long id); } package cn.itcast.cd.daoImpl; import java.sql.SQLException; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.ResultSetHandler; import cn.itcast.cd.Utils.Utils; public class BaseDao { // 使用DbUtils中的QueryRunner对象. private QueryRunner queryRunner = new QueryRunner(Utils.getDataSource()); /* * 执行修改 */ public void exeUpdate(String sql, Object... params) { try { queryRunner.update(sql, params); } catch (SQLException e) { e.printStackTrace(); } } /* * 执行查询 */ public <T> T exeQuery(String sql, ResultSetHandler<T> rsh, Object... params) { try { return queryRunner.query(sql, rsh, params); } catch (SQLException e) { e.printStackTrace(); } return null; } } package cn.itcast.cd.daoImpl; import java.util.List; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import cn.itcast.cd.dao.IUserDAO; import cn.itcast.cd.domain.User; public class UserDAOImpl extends BaseDao implements IUserDAO { @Override public void add(User user) { String sql = "insert into user values(null, ?, ?, ?, ?, ?, ?)"; Object[] params = { user.getName(), user.getSex(), user.getPic(), user.getIp(), user.getFileName(), user.getFilePath() }; exeUpdate(sql, params); } @Override public List<User> list() { String sql = "select * from user"; return exeQuery(sql, new BeanListHandler<User>(User.class)); } @Override public User get(Long id) { String sql = "select * from user where id=?"; return exeQuery(sql, new BeanHandler<User>(User.class), id); } } package cn.itcast.cd.servlet; import java.io.IOException; import java.lang.reflect.Method; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; public class BaseServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); String mode = req.getParameter("mode"); if (StringUtils.isNotBlank(mode)) { try { Method method = this.getClass().getMethod(mode, HttpServletRequest.class, HttpServletResponse.class); method.invoke(this, req, resp); } catch (Exception e) { e.printStackTrace(); } } else { doMethod(req, resp); // 默认调用 } } protected void doMethod(HttpServletRequest req, HttpServletResponse resp) { } } package cn.itcast.cd.servlet; import java.io.File; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.io.FilenameUtils; import cn.itcast.cd.Utils.MyException; import cn.itcast.cd.Utils.UploadFileInfo; public class MyHttpServletRequest extends HttpServletRequestWrapper { // 装在普通表单元素的map private Map<String, String> formFiledMap = new HashMap<String, String>(); // 装在上传表单元素的Map private Map<String, FileItem> uploadFieldMap = new HashMap<String, FileItem>(); Boolean isMultipartContent = false; public MyHttpServletRequest(HttpServletRequest request) { super(request); // 检查是否是文件上传请求 isMultipartContent = ServletFileUpload.isMultipartContent(request); if (isMultipartContent) { // 创建工程,设置上传最大的内存空间和临时文件存放位置. FileItemFactory factory = new DiskFileItemFactory(1024 * 1024 * 10, new File("D:/tmp")); ServletFileUpload fileUpload = new ServletFileUpload(factory); fileUpload.setFileSizeMax(1024 * 1024); // 设置单个文件上传的最大值为1M. fileUpload.setSizeMax(1024 * 1024 * 5); // 设置整个表单的文件上传最大值为5M. try { List<FileItem> fileItems = fileUpload.parseRequest(request); for (FileItem fileItem : fileItems) { // 是否是普通表单元素,即type不为file的表单元素 if (fileItem.isFormField()) { formFiledMap.put(fileItem.getFieldName(), fileItem.getString("UTF-8")); } else { if (fileItem.getSize() > 0) { uploadFieldMap.put(fileItem.getFieldName(), fileItem); } } } } catch (SizeLimitExceededException e) { throw new MyException("上传文件的大小超过限制!"); } catch (FileUploadException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } // 包装原来的方法 @Override public String getParameter(String name) { // 是上传文件的表单,要得到参数值,直接根据name,取存在map中的值. if (isMultipartContent) { return formFiledMap.get(name); } else { return super.getParameter(name); } } // 已经将Map修改了,覆盖原来的方法 @Override public Map getParameterMap() { if (isMultipartContent) { return formFiledMap; } else { return super.getParameterMap(); } } /** * 上传表单中指定表单元素中的文件. * * @param formFieldName * 表单中元素的名称 * @param tagPath * 上传的目标目录,即服务器上存放上传文件的目录. * * @return 由于上传成功后,我们需要将上传的文件名和文件地址存放到数据库, * 也即需要返回给Servlet上传的文件信息,故将文件信息封装成一个对象返回. * */ public UploadFileInfo upload(String formFieldName, String tagPath) { FileItem fileItem = uploadFieldMap.get(formFieldName); if (fileItem != null) { // 得到文件名fileItem.getName()有些浏览器可能得到的是全路径,而这里只需要文件名 String srcFileName = FilenameUtils.getName(fileItem.getName()); long fileSize = fileItem.getSize(); //不同的用户可能上传相同的文件名,不处理就被覆盖,所以这里需要用唯一标示进行文件区分. String tagFilePath = tagPath + "/" + UUID.randomUUID().toString() + "." + FilenameUtils.getExtension(srcFileName); if (!tagPath.startsWith("/")){ tagPath = "/" + tagPath; } //判断目录是否存在,不存在就创建 File dir = new File(getSession().getServletContext().getRealPath("/") + tagPath); if (!dir.exists()) { dir.mkdirs(); } //写到指定目录 try { fileItem.write(new File(getSession().getServletContext().getRealPath("/"), tagFilePath)); } catch (Exception e) { e.printStackTrace(); } return new UploadFileInfo(srcFileName, tagFilePath, fileSize); } return null; } } package cn.itcast.cd.servlet; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.List; import java.util.UUID; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.catalina.startup.SetAllPropertiesRule; import org.apache.commons.lang3.StringUtils; import cn.itcast.cd.Utils.MyException; import cn.itcast.cd.Utils.UploadFileInfo; import cn.itcast.cd.dao.IUserDAO; import cn.itcast.cd.daoImpl.UserDAOImpl; import cn.itcast.cd.domain.User; /** * Servlet implementation class UploadServlet */ public class UploadServlet extends BaseServlet { private static final long serialVersionUID = 1L; IUserDAO dao = null; @Override public void init() throws ServletException { dao = new UserDAOImpl(); } public void add(HttpServletRequest request, HttpServletResponse response) throws InterruptedException { // request不能接收上传文件的表单参数,返回全部为null,所以用MyHttpServletRequest类进行包装,单独处理. // Thread.sleep(1000); MyHttpServletRequest myRequest = null; try { myRequest = new MyHttpServletRequest(request); } catch (MyException e) { // 捕捉自定义异常,提示用户上传文件的大小限制 request.setAttribute("error", e.getMessage()); upload(request, response); return; } /* * 从页面得到标示信息,由于是文件上传的请求,必须用myRequest */ String randomName = myRequest.getParameter("randomName"); String randomValue = myRequest.getParameter("randomValue"); //session中的标示. String randomValueInSession = (String) myRequest.getSession().getAttribute(randomName); // System.out.println(randomName + " " + randomValue + " " + randomValueInSession); if(StringUtils.isNotBlank(randomValueInSession) && StringUtils.isNotBlank(randomValue) && StringUtils.isNotBlank(randomName) && randomValueInSession.equals(randomValue)){ // 接收普通表单元素的参数信息 String name = myRequest.getParameter("name"); String sex = myRequest.getParameter("sex"); // 封装对象 User user = new User(); user.setName(name); user.setSex(Boolean.parseBoolean(sex)); String ip = request.getRemoteAddr(); user.setIp(ip); // 上传头像信息 UploadFileInfo picInfo = myRequest.upload("pic", "/headPic"); if (picInfo != null) { user.setPic(picInfo.getTagFilePath()); } UploadFileInfo fileInfo = myRequest.upload("fileName", "/WEB-INF/resource"); if (fileInfo != null) { user.setFilePath(fileInfo.getTagFilePath()); user.setFileName(fileInfo.getSrcFileName()); user.setFileSize(fileInfo.getFileSize()); } dao.add(user); //提交完成移除session中设置的随机标示 request.getSession().removeAttribute(randomName); } else { request.setAttribute("error", "重复提交请求……"); } list(request, response); } public void list(HttpServletRequest request, HttpServletResponse response) { List<User> list = dao.list(); request.setAttribute("list", list); try { request.getRequestDispatcher("/WEB-INF/jsp/list.jsp").forward( request, response); } catch (ServletException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void upload(HttpServletRequest request, HttpServletResponse response) { try { //防止重复提交 String randomName = UUID.randomUUID().toString(); String randomValue = UUID.randomUUID().toString(); request.setAttribute("randomName", randomName ); request.setAttribute("randomValue", randomValue); //当前产生的标示放到session. request.getSession().setAttribute(randomName, randomValue); request.getRequestDispatcher("/WEB-INF/jsp/upload.jsp").forward( request, response); } catch (ServletException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Override protected void doMethod(HttpServletRequest request, HttpServletResponse response) { list(request, response); } public void download(HttpServletRequest request, HttpServletResponse response) { String id = request.getParameter("id"); User user = dao.get(Long.parseLong(id)); // 发送下载请求给浏览器 response.setContentType("application/x-msdownload"); try { // 解决下载时文件名乱码问题. response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(user.getFileName(), "utf-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } BufferedOutputStream bos = null; BufferedInputStream bis = null; try { bos = new BufferedOutputStream(response.getOutputStream()); bis = new BufferedInputStream(new FileInputStream(new File( getServletContext().getRealPath("/"), user.getFilePath()))); byte[] b = new byte[1024]; int lenth = 0; while ((lenth = bis.read(b)) != -1) { bos.write(b, 0, lenth); bos.flush(); } } catch (IOException e) { e.printStackTrace(); } finally { try { bos.close(); bis.close(); } catch (IOException e) { e.printStackTrace(); } } } } package cn.itcast.cd.Utils; public class MyException extends RuntimeException { public MyException(String message){ super(message); } } package cn.itcast.cd.Utils; public class UploadFileInfo { private String srcFileName; private String tagFilePath; private long fileSize; public String getSrcFileName() { return srcFileName; } public void setSrcFileName(String srcFileName) { this.srcFileName = srcFileName; } public String getTagFilePath() { return tagFilePath; } public void setTagFilePath(String tagFilePath) { this.tagFilePath = tagFilePath; } public long getFileSize() { return fileSize; } public void setFileSize(long fileSize) { this.fileSize = fileSize; } public UploadFileInfo(String srcFileName, String tagFilePath, long fileSize) { super(); this.srcFileName = srcFileName; this.tagFilePath = tagFilePath; this.fileSize = fileSize; } } package cn.itcast.cd.Utils; import java.util.Properties; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSourceFactory; public class Utils { private static DataSource dataSource; static{ Properties properties = new Properties(); try { properties.load(Utils.class.getClassLoader().getResourceAsStream("jdbc.properties")); dataSource = BasicDataSourceFactory.createDataSource(properties); } catch (Exception e) { e.printStackTrace(); } } public static DataSource getDataSource(){ return dataSource; } }