1、up.sql
use mydb3; drop table if exists up; create table if not exists up( id int primary key auto_increment, username varchar(20) not null, realFileName varchar(40) not null, uuidFileName varchar(80) not null );
2、UpDao.java
package com.gnnuit.web.updownload.dao; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import com.gnnuit.web.updownload.domain.Up; import com.gnnuit.web.updownload.util.JdbcUtil; public class UpDao { // 增加Up对象 public void addUp(Up up) throws SQLException { QueryRunner runner = new QueryRunner(); String sql = "insert into up(username,realFileName,uuidFileName) values(?,?,?)"; runner.update( JdbcUtil.getMySqlConnection(), sql, new Object[] { up.getUsername(), up.getRealFileName(), up.getUuidFileName() }); } // 查询所有的Up对象 public List<Up> findAllUp() throws SQLException { List<Up> upList = new ArrayList<Up>(); QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource()); String sql = "select * from up"; upList = (List<Up>) runner .query(sql, new BeanListHandler<Up>(Up.class)); return upList; } // 根据UUID文件名查询唯一的Up对象 public Up findUpByUuidFileName(String uuidFileName) throws SQLException { Up up = null; QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource()); String sql = "select * from up where uuidFileName = ?"; up = (Up) runner .query(sql, new BeanHandler<Up>(Up.class), uuidFileName); return up; } }
3、Up.java
package com.gnnuit.web.updownload.domain; public class Up { private int id; private String username; private String realFileName; private String uuidFileName; public Up() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getRealFileName() { return realFileName; } public void setRealFileName(String realFileName) { this.realFileName = realFileName; } public String getUuidFileName() { return uuidFileName; } public void setUuidFileName(String uuidFileName) { this.uuidFileName = uuidFileName; } }
4、User.java
package com.gnnuit.web.updownload.domain; import java.util.ArrayList; import java.util.List; import org.apache.commons.fileupload.FileItem; //封装上传文件的内容 public class User { // 上传用户 private String username; // 上传的文件 private List<FileItem> upfileList = new ArrayList<FileItem>(); public User() { } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public List<FileItem> getUpfileList() { return upfileList; } public void setUpfileList(List<FileItem> upfileList) { this.upfileList = upfileList; } }
5、JdbcUtil.java
package com.gnnuit.web.updownload.util; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.mchange.v2.c3p0.ComboPooledDataSource; //JDBC工具类:关闭流和取得连接 public final class JdbcUtil { private static ComboPooledDataSource dataSource; private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>(); static { dataSource = new ComboPooledDataSource(); } // 取得数据源 public static ComboPooledDataSource getDataSource() { return dataSource; } // 取得连接 public static Connection getMySqlConnection() throws SQLException { Connection conn = tl.get(); if (conn == null) { conn = dataSource.getConnection(); tl.set(conn); } return conn; } // 关闭连接 public static void close(Connection conn) throws SQLException { if (conn != null) { conn.close(); } } public static void close(PreparedStatement pstmt) throws SQLException { if (pstmt != null) { pstmt.close(); } } public static void close(ResultSet rs) throws SQLException { if (rs != null) { rs.close(); } } public static void begin() throws SQLException { Connection conn = getMySqlConnection(); conn.setAutoCommit(false); } public static void commit() throws SQLException { Connection conn = getMySqlConnection(); conn.commit(); } public static void rollback() throws SQLException { Connection conn = getMySqlConnection(); conn.rollback(); } public static void closeConnection() throws SQLException { Connection conn = getMySqlConnection(); close(conn); tl.remove(); } }
6、UploadUtil.java
package com.gnnuit.web.updownload.util; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.List; import java.util.UUID; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import com.gnnuit.web.exception.NoUpfileException; import com.gnnuit.web.updownload.domain.Up; import com.gnnuit.web.updownload.domain.User; public final class UploadUtil { // 取得上传使用的临时和真实目录 public static final String tempPath = "/WEB-INF/temp"; public static final String uploadPath = "/WEB-INF/upload"; // 取得真实文件名 public static String getRealFileName(String realFileName) { int index = realFileName.lastIndexOf("\"); if (index >= 0) { // IE6浏览器 realFileName = realFileName.substring(index + 1); } return realFileName; } // 取得upload/目录下的分散目录 public static String makeUuidFilePath(String uploadPath, String uuidFileName) { String uuidFilePath = null; int code = uuidFileName.hashCode();// 8 int dir1 = code & 0xF;// 3 int dir2 = code >> 4 & 0xF;// A File file = new File(uploadPath + "/" + dir1 + "/" + dir2); // 如果该目录未存在 if (!file.exists()) { // 一次性创建N层目录 file.mkdirs(); } uuidFilePath = file.getPath(); return uuidFilePath; } // 取得uuid文件名 public static String makeUuidFileName(String realFileName) { return UUID.randomUUID().toString() + "_" + realFileName; } // 文件复制 public static void doSave(InputStream is, String uuidFileName, String uuidFilePath) { OutputStream os = null; try { os = new FileOutputStream(uuidFilePath + "/" + uuidFileName); byte[] buf = new byte[1024]; int len = 0; while ((len = is.read(buf)) > 0) { os.write(buf, 0, len); } } catch (Exception e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } } // 将上传文件封装成JavaBean对象中 public static User doUpload(HttpServletRequest request, List<Up> upList) throws Exception { User user = new User(); // 创建上传文件工厂 DiskFileItemFactory factory = new DiskFileItemFactory(); // 设置内存中缓存区的大小,默认10K factory.setSizeThreshold(100 * 1024); // 设置上传文件临时存放的目录 String tempPath = request.getSession().getServletContext() .getRealPath(UploadUtil.tempPath); factory.setRepository(new File(tempPath)); // 创建上传文件对象[核心] ServletFileUpload upload = new ServletFileUpload(factory); // 设置上传文件的中文编码方式 upload.setHeaderEncoding("UTF-8"); // 客户端上传文件是否使用MIME协议, boolean flag = ServletFileUpload.isMultipartContent(request); if (!flag) { // 不是以MIME协议上传文件 throw new ServletException(); } else { /* * 是以MIME协议上传的文件,解析request中的所有上传内容每个内容封装成一个对象FileItem, * FileItem代表普通字段和上传字段二类 */ List<FileItem> fileItemList = upload.parseRequest(request); for (FileItem fileItem : fileItemList) { if (fileItem.isFormField()) { // 必定是普通字段 String fieldName = fileItem.getFieldName(); String fieldValue = fileItem.getString("UTF-8"); user.setUsername(fieldValue); } else { // 必定是上传字段 // 如果无上传文件 if (fileItem.getSize() == 0) { throw new NoUpfileException(); } String realFileName = UploadUtil.getRealFileName(fileItem .getName()); /* * 只能上传JPG文件 if(!realFileName.endsWith("JPG")){ throw new * UpfileTypeException(); } */ /* * 只有上传<=200K的文件 if(fileItem.getSize() > 200 * 1024){ throw * new UpfileSizeException(); } */ // 封装到JavaBean user.getUpfileList().add(fileItem); // 封装N个Up对象 Up up = new Up(); up.setRealFileName(realFileName); up.setUuidFileName(UploadUtil .makeUuidFileName(realFileName)); up.setUsername(user.getUsername()); upList.add(up); } }// end of for loop } return user; } public static void doSave(User user, String uploadPath, List<Up> upList) throws Exception { // 取得该用户上传的所有文件集合 List<FileItem> fileItemList = user.getUpfileList(); // 迭代每个文件,并上传 int index = 0; for (FileItem fileItem : fileItemList) { // 取得输入流 InputStream is = fileItem.getInputStream(); // 取得真实文件名 String realFileName = fileItem.getName(); realFileName = UploadUtil.getRealFileName(realFileName); // 取得UUID文件名 // String uuidFileName = UploadUtil.makeUuidFileName(realFileName); String uuidFileName = upList.get(index).getUuidFileName(); // 取得UUID文件路径 String uuidFilePath = UploadUtil.makeUuidFilePath(uploadPath, uuidFileName); // 保存 UploadUtil.doSave(is, uuidFileName, uuidFilePath); // 删除临时文件 fileItem.delete(); index++; } } }
7、UpService.java
package com.gnnuit.web.updownload.service; import java.sql.SQLException; import java.util.List; import com.gnnuit.web.updownload.dao.UpDao; import com.gnnuit.web.updownload.domain.Up; public class UpService { private UpDao upDao = new UpDao(); public void addUps(List<Up> upList) throws Exception { for (Up up : upList) { try { upDao.addUp(up); } catch (SQLException e) { e.printStackTrace(); throw new Exception(); } } } public List<Up> findAllUp() throws Exception { try { return upDao.findAllUp(); } catch (SQLException e) { e.printStackTrace(); throw new Exception(); } } public Up findUpByUuidFileName(String uuidFileName) throws Exception { try { return upDao.findUpByUuidFileName(uuidFileName); } catch (SQLException e) { e.printStackTrace(); throw new Exception(); } } }
8、UploadServlet.java
package com.gnnuit.web.updownload.upload; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.gnnuit.web.updownload.domain.Up; import com.gnnuit.web.updownload.domain.User; import com.gnnuit.web.updownload.exception.NoUpfileException; import com.gnnuit.web.updownload.exception.UpfileSizeException; import com.gnnuit.web.updownload.exception.UpfileTypeException; import com.gnnuit.web.updownload.service.UpService; import com.gnnuit.web.updownload.util.JdbcUtil; import com.gnnuit.web.updownload.util.UploadUtil; public class UploadServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { String uploadPath = this.getServletContext().getRealPath( UploadUtil.uploadPath); List<Up> upList = new ArrayList<Up>(); User user = UploadUtil.doUpload(request, upList); // 事务开始 JdbcUtil.begin(); // 写入表 UpService upService = new UpService(); upService.addUps(upList); // int i = 10/0; // 写入硬盘 UploadUtil.doSave(user, uploadPath, upList); // 事务提交 JdbcUtil.commit(); request.setAttribute("message", "上传文件成功"); request.getRequestDispatcher("/WEB-INF/message.jsp").forward( request, response); } catch (UpfileSizeException e) { e.printStackTrace(); request.setAttribute("message", "<font color='green'>上传文件大小限制在200K以内</font>"); request.getRequestDispatcher("/WEB-INF/message.jsp").forward( request, response); } catch (UpfileTypeException e) { e.printStackTrace(); request.setAttribute("message", "<font color='red'>只能上传JPG格式的文件</font>"); request.getRequestDispatcher("/WEB-INF/message.jsp").forward( request, response); } catch (NoUpfileException e) { e.printStackTrace(); request.setAttribute("message", "<font color='blue'>无上传文件</font>"); request.getRequestDispatcher("/WEB-INF/message.jsp").forward( request, response); } catch (Exception e) { // 事务回滚 try { JdbcUtil.rollback(); JdbcUtil.commit(); } catch (Exception e1) { e1.printStackTrace(); } // 事务提交 e.printStackTrace(); request.setAttribute("message", "上传文件失败"); request.getRequestDispatcher("/WEB-INF/message.jsp").forward( request, response); } finally { // 关闭Connection对象,并分离线程 try { JdbcUtil.closeConnection(); } catch (Exception e) { e.printStackTrace(); } } } }
9、DownloadServlet.java
package com.gnnuit.web.updownload.download; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URLEncoder; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.gnnuit.web.updownload.domain.Up; import com.gnnuit.web.updownload.service.UpService; import com.gnnuit.web.util.UploadUtil; //下载文件到本地[DB版] public class DownloadServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 取得uuid文件名并进行URL解码 String uuidFileName = request.getParameter("uuidFileName"); byte[] buf = uuidFileName.getBytes("ISO8859-1"); uuidFileName = new String(buf, "UTF-8"); try { // 根据UUID文件名查询Up对象 UpService upService = new UpService(); Up up = upService.findUpByUuidFileName(uuidFileName); // 向浏览器输出以下载方式打开文件 response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(up.getRealFileName(), "UTF-8")); // 真正下载 String uploadPath = this.getServletContext().getRealPath( UploadUtil.uploadPath); String uuidFilePath = UploadUtil.makeUuidFilePath(uploadPath, uuidFileName); InputStream is = new FileInputStream(uuidFilePath + "/" + uuidFileName); OutputStream os = response.getOutputStream(); int len = 0; buf = new byte[1024]; while ((len = is.read(buf)) > 0) { os.write(buf, 0, len); } is.close(); os.close(); } catch (Exception e) { e.printStackTrace(); request.setAttribute("message", "下载文件失败"); request.getRequestDispatcher("/WEB-INF/message.jsp").forward( request, response); } } }
10、ListFileServlet.java
package com.gnnuit.web.updownload.download; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.gnnuit.web.updownload.domain.Up; import com.gnnuit.web.updownload.service.UpService; //显示可供下载的文件列表[DB版] public class ListFileServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { UpService upService = new UpService(); List<Up> upList = upService.findAllUp(); request.setAttribute("upList", upList); request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request, response); } catch (Exception e) { e.printStackTrace(); request.setAttribute("message", "显示可供下载文件失败"); request.getRequestDispatcher("/WEB-INF/message.jsp").forward( request, response); } } }
11、NoUpfileException.java
package com.gnnuit.web.updownload.exception; public class NoUpfileException extends Exception { private static final long serialVersionUID = 1L; }
12、UpfileSizeException.java
package com.gnnuit.web.updownload.exception; public class UpfileSizeException extends Exception { private static final long serialVersionUID = 1L; }
13、UpfileTypeException.java
package com.gnnuit.web.updownload.exception; public class UpfileTypeException extends Exception { private static final long serialVersionUID = 1L; }
14、c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="user">root</property> <property name="password">root</property> <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/mydb3</property> </default-config> </c3p0-config>
15、upload.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type="text/javascript"> //全局变量 var time = 0; function addLine(addButton){ //创建内部div对象 var divElement = document.createElement("div"); //创建input对象[file类型] var inputElement1 = document.createElement("input"); inputElement1.type="file"; inputElement1.name="upfile"; //创建input对象[button类型] var inputElement2 = document.createElement("input"); inputElement2.type="button"; inputElement2.value="删除"; //对删除按钮添加事件监听 inputElement2.onclick=function(){ //取得该按钮所在行的直接父元素 var divElement = this.parentNode.parentNode; //通过父元素删除直接子元素 divElement.removeChild(this.parentNode); time--; if(time < 5){ //按钮生效 addButton.disabled=false; //addButton.style.visibility="visible"; } }; //依次将file类型和button类型的input对象加入到内部div对象中 divElement.appendChild(inputElement1); divElement.appendChild(inputElement2); //再次内部div对象加入到外部div对象 var outDivElement = document.getElementById("outDiv"); outDivElement.appendChild(divElement); time++; if(time == 5){ //将按钮失效 addButton.disabled=true; //addButton.style.visibility="hidden"; } } </script> </head> <body> <form action="${pageContext.request.contextPath}/UploadServlet" method="POST" enctype="multipart/form-data"> <table border="1" align="center"> <caption>文件上传</caption> <tr> <th>上传用户</th> <td><input type="text" name="username"/></td> </tr> <tr> <th></th> <td> <div id="outDiv"> <%-- <div> <input type="file" name="upfile"/> <input type="button" value="删除"/> </div> --%> </div> </td> </tr> <tr> <th></th> <td> <input type="button" value="添加上传文件" onclick="addLine(this)" /> </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="上传"/> <a href="${pageContext.request.contextPath}/ListFileServlet"> 显示下载文件 </a> </td> </tr> </table> </form> </body> </html>
16、download.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> <table align="center" border="1"> <tr> <th>文件列表</th> <th>操作</th> </tr> <c:forEach var="entry" items="${map }"> <tr> <td>${entry.value }</td> <td> <c:url var="myURL" value="/DownLoadServlet"> <c:param name="fileName" value="${entry.key }"></c:param> </c:url> <a href="${myURL }" >下载</a> </td> </tr> </c:forEach> </table> </body> </html>
17、list.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> <table border="1" align="center"> <caption>下载文件列表</caption> <tr> <th>文件名</th> <th>操作</th> </tr> <c:forEach var="up" items="${requestScope.upList}"> <tr> <td>${up.realFileName}</td> <td> <c:url var="myURL" value="/DownloadServlet"> <c:param name="uuidFileName" value="${up.uuidFileName}"/> </c:url> <a href="${myURL}" style="text-decoration:none"> 下载 </a> </td> </tr> </c:forEach> </table> </body> </html>
18、message.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> ${message}<br/> <a href="${pageContext.request.contextPath}/upload.jsp"> 返回 </a> </body> </html>
19、web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>day18</display-name> <servlet> <servlet-name>Upload</servlet-name> <servlet-class>com.gnnuit.web.servlet.upload.Upload</servlet-class> </servlet> <servlet-mapping> <servlet-name>Upload</servlet-name> <url-pattern>/Upload</url-pattern> </servlet-mapping> <servlet> <servlet-name>UploadServlet</servlet-name> <servlet-class>com.gnnuit.web.updownload.upload.UploadServlet</servlet-class> </servlet> <servlet> <servlet-name>ListFileServlet</servlet-name> <servlet-class>com.gnnuit.web.updownload.download.ListFileServlet</servlet-class> </servlet> <servlet> <servlet-name>DownloadServlet</servlet-name> <servlet-class>com.gnnuit.web.updownload.download.DownloadServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>UploadServlet</servlet-name> <url-pattern>/UploadServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ListFileServlet</servlet-name> <url-pattern>/ListFileServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>DownloadServlet</servlet-name> <url-pattern>/DownloadServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>