Jsp+Servlet实现文件上传下载 文件列表展示(二)

接着上一篇讲:
Jsp+Servlet实现文件上传下载(一)--文件上传

本章来实现一下上传文件列表展示,同时优化了一下第一章中的代码。

废话少说,上代码

mysql创建附件表

drop TABLE tbl_accessory; 
 
CREATE TABLE tbl_accessory 
( 
 id INT AUTO_INCREMENT PRIMARY KEY, 
 file_name VARCHAR(500), 
 file_size DOUBLE(10,2), 
 file_ext_name VARCHAR(100), 
 file_path VARCHAR(2000) 
) 
; 
 
select * FROM tbl_accessory; 
 
delete FROM tbl_accessory; 

创建附件实体类

package entity.upload; 
 
/** 
 * 附件实体类 
 * 
 * @author xusucheng 
 * @create 2017-12-29 
 **/ 
public class EntityAccessory { 
 private int id; 
 private String fileName; 
 private double fileSize; 
 private String file_ext_name; 
 private String filePath; 
 
 public int getId() { 
  return id; 
 } 
 
 public void setId(int id) { 
  this.id = id; 
 } 
 
 public String getFileName() { 
  return fileName; 
 } 
 
 public void setFileName(String fileName) { 
  this.fileName = fileName; 
 } 
 
 public double getFileSize() { 
  return fileSize; 
 } 
 
 public void setFileSize(double fileSize) { 
  this.fileSize = fileSize; 
 } 
 
 public String getFile_ext_name() { 
  return file_ext_name; 
 } 
 
 public void setFile_ext_name(String file_ext_name) { 
  this.file_ext_name = file_ext_name; 
 } 
 
 public String getFilePath() { 
  return filePath; 
 } 
 
 public void setFilePath(String filePath) { 
  this.filePath = filePath; 
 } 
} 

创建DBUtil工具类

package util; 
 
import java.sql.*; 
import java.io.InputStream; 
import java.util.Properties; 
 
/** 
 * 数据库工具类 
 * 
 * @author xusucheng 
 * @create 2017-11-18 
 **/ 
public class DBUtil { 
 //定义链接所需要的变量 
 private static Connection con = null; 
 private static PreparedStatement ps = null; 
 private static ResultSet rs = null; 
 
 //定义链接数据库所需要的参数 
 private static String url = ""; 
 private static String username = ""; 
 private static String driver=""; 
 private static String password=""; 
 
 //定义读取配置文件所需要的变量 
 private static Properties pp = null; 
 private static InputStream fis = null; 
 
 /** 
  * 加载驱动 
  */ 
 static { 
  try { 
   //从dbinfo.properties配置文件中读取配置信息 
   pp = new Properties(); 
   fis = DBUtil.class.getClassLoader().getResourceAsStream("db.properties"); 
 
   pp.load(fis); 
   url = pp.getProperty("url"); 
   username = pp.getProperty("username"); 
   driver=pp.getProperty("driver"); 
   password=pp.getProperty("password"); 
 
   //加载驱动 
   Class.forName(driver); 
 
  } catch (Exception e) { 
   System.out.println("驱动加载失败!"); 
   e.printStackTrace(); 
  } finally { 
   try { 
    fis.close(); 
   } catch (Exception e) { 
    e.printStackTrace(); 
   } 
 
   fis = null; //垃圾回收自动处理 
  } 
 
 } 
 
 /** 
  * 得到Connection链接 
  * @return Connection 
  */ 
 public static Connection getConnection() { 
 
  try { 
   //建立连接 
   con = DriverManager.getConnection(url, username, password); 
 
  } catch (Exception e) { 
   System.out.println("数据库链接失败!"); 
   e.printStackTrace(); 
  } 
 
  return con; 
 } 
 
 /*public DBUtil(String sql){ 
  try { 
   ps = getConnection().prepareStatement(sql);//准备执行语句 
  } catch (Exception e) { 
   e.printStackTrace(); 
  } 
 } 
 
 public void close() { 
  try { 
   con.close(); 
   ps.close(); 
  } catch (SQLException e) { 
   e.printStackTrace(); 
  } 
 }*/ 
 
 /** 
  * 统一的资源关闭函数 
  * @param rs 
  * @param ps 
  * @param con 
  */ 
 public static void close(ResultSet rs,Statement ps, Connection con){ 
 
  if(rs != null) { 
   try { 
    rs.close(); 
   } catch (Exception e) { 
    e.printStackTrace(); 
   } 
  } 
  if(ps != null) { 
   try { 
    ps.close(); 
   } catch (Exception e) { 
    e.printStackTrace(); 
   } 
  } 
  if(con != null) { 
   try { 
    con.close(); 
   } catch (Exception e) { 
    e.printStackTrace(); 
   } 
  } 
 } 
 
} 

创建附件实体DAO类

package dao.upload; 
 
import entity.upload.EntityAccessory; 
import util.DBUtil; 
 
import java.math.BigDecimal; 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.ArrayList; 
import java.util.List; 
 
/** 
 * 附件上传DAO 
 * 
 * @author xusucheng 
 * @create 2017-12-29 
 **/ 
public class AccessoryDao { 
 public static void add(EntityAccessory entity) { 
  Connection conn = DBUtil.getConnection(); 
  String sql = "insert into tbl_accessory(file_name,file_size,file_ext_name,file_path) values(?,?,?,?)"; 
  try { 
   PreparedStatement ps = conn.prepareStatement(sql); 
   ps.setString(1, entity.getFileName()); 
   ps.setDouble(2, entity.getFileSize()); 
   ps.setString(3, entity.getFile_ext_name()); 
   ps.setString(4, entity.getFilePath()); 
   ps.execute(); 
   //conn.commit(); 
 
   DBUtil.close(null, ps, conn); 
  } catch (SQLException e) { 
   e.printStackTrace(); 
  } 
 } 
 
 public static List<EntityAccessory> list() { 
  Connection conn = DBUtil.getConnection(); 
  String sql = "select id,file_name,file_size,file_ext_name,file_path from tbl_accessory"; 
  List<EntityAccessory> accessoryList = new ArrayList<>(); 
  try { 
   PreparedStatement ps = conn.prepareStatement(sql); 
   ResultSet rs = ps.executeQuery(); 
 
   while (rs.next()) { 
    EntityAccessory entity = new EntityAccessory(); 
    entity.setId(rs.getInt("id")); 
    entity.setFileName(rs.getString("file_name")); 
    entity.setFileSize(new BigDecimal(rs.getDouble("file_size") / 1024).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()); 
    entity.setFile_ext_name(rs.getString("file_ext_name")); 
    entity.setFilePath(rs.getString("file_path")); 
    accessoryList.add(entity); 
   } 
 
   DBUtil.close(rs, ps, conn); 
  } catch (SQLException e) { 
   e.printStackTrace(); 
  } 
 
  return accessoryList; 
 
 } 
 
 public static void remove(int id) { 
  Connection conn = DBUtil.getConnection(); 
  String sql = "delete from tbl_accessory where id=?"; 
  try { 
   PreparedStatement ps = conn.prepareStatement(sql); 
   ps.setInt(1,id); 
   ps.execute(); 
   //conn.commit(); mysql默认开启了autocommit 
 
   DBUtil.close(null,ps,conn); 
  } catch (SQLException e) { 
   e.printStackTrace(); 
  } 
 } 
} 

创建list.jsp列表页面

<html> 
<head> 
 <title>上传文件列表</title> 
</head> 
<body> 
 
<h3>文件列表</h3> 
<table class="acclist_tab" border="1" bordercolor="#000000" cellspacing="0" cellpadding="2" style="border-collapse:collapse;"> 
 <tr> 
  <th>文件名</th> 
  <th>文件大小(KB)</th> 
  <th>操作</th> 
 </tr> 
 <c:if test="${not empty accessoryList}"> 
  <c:forEach items="${accessoryList}" var="acc"> 
   <tr> 
    <td>${acc.fileName}</td> 
    <td>${acc.fileSize}</td> 
    <td><a href="">删除</a></td> 
   </tr> 
  </c:forEach> 
 </c:if> 
</table> 
</body> 
</html> 

创建展示列表Servlet:listUploadedFilesServlet

package servlet.upload; 
 
import dao.upload.AccessoryDao; 
import entity.upload.EntityAccessory; 
 
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 java.io.IOException; 
import java.util.List; 
 
/** 
 * 返回已上传文件列表 
 * 
 * @author xusucheng 
 * @create 2017-12-29 
 **/ 
 
@WebServlet("/listUploadedFiles") 
public class listUploadedFilesServlet extends HttpServlet { 
 @Override 
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  //获取文件列表 
  List<EntityAccessory> accessoryList = AccessoryDao.list(); 
  request.setAttribute("accessoryList", accessoryList); 
 
  request.getRequestDispatcher("pages/upload/list.jsp").forward(request, response); 
 } 
 
 @Override 
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  doPost(request, response); 
 } 
} 

增加error.jsp显示上传失败信息

<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<html> 
<head> 
 <title>上传错误页面</title> 
</head> 
<body> 
 
<h3>上传失败:</h3> 
<c:if test="${not empty errorMessage}"> 
 <%--<input type="text" id="errorMessage" value="${errorMessage}" style="color:red;" disabled="disabled">--%> 
 <h4 style="color: red;">${errorMessage}</h4> 
</c:if> 
 
 
</body> 
</html> 

优化了第一章中的上传控制器

package servlet.upload; 
 
import dao.upload.AccessoryDao; 
import entity.upload.EntityAccessory; 
import org.apache.commons.fileupload.FileItem; 
import org.apache.commons.fileupload.FileUploadBase; 
import org.apache.commons.fileupload.FileUploadException; 
import org.apache.commons.fileupload.ProgressListener; 
import org.apache.commons.fileupload.disk.DiskFileItemFactory; 
import org.apache.commons.fileupload.servlet.ServletFileUpload; 
 
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 java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.Calendar; 
import java.util.Iterator; 
import java.util.List; 
import java.util.UUID; 
 
/** 
 * 处理文件上传 
 * 
 * @author xusucheng 
 * @create 2017-12-27 
 **/ 
@WebServlet("/UploadServlet") 
public class UploadServlet extends HttpServlet { 
 @Override 
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  //设置文件上传基本路径 
  String savePath = this.getServletContext().getRealPath("/WEB-INF/uploadFiles"); 
  //设置临时文件路径 
  String tempPath = this.getServletContext().getRealPath("/WEB-INF/tempFiles"); 
  File tempFile = new File(tempPath); 
  if (!tempFile.exists()) { 
   tempFile.mkdir(); 
  } 
 
  //定义异常消息 
  String errorMessage = ""; 
  //创建file items工厂 
  DiskFileItemFactory factory = new DiskFileItemFactory(); 
  //设置缓冲区大小 
  factory.setSizeThreshold(1024 * 100); 
  //设置临时文件路径 
  factory.setRepository(tempFile); 
  //创建文件上传处理器 
  ServletFileUpload upload = new ServletFileUpload(factory); 
  //监听文件上传进度 
  ProgressListener progressListener = new ProgressListener() { 
   public void update(long pBytesRead, long pContentLength, int pItems) { 
    System.out.println("正在读取文件: " + pItems); 
    if (pContentLength == -1) { 
     System.out.println("已读取: " + pBytesRead + " 剩余0"); 
    } else { 
     System.out.println("文件总大小:" + pContentLength + " 已读取:" + pBytesRead); 
    } 
   } 
  }; 
  upload.setProgressListener(progressListener); 
 
  //解决上传文件名的中文乱码 
  upload.setHeaderEncoding("UTF-8"); 
  //判断提交上来的数据是否是上传表单的数据 
  if (!ServletFileUpload.isMultipartContent(request)) { 
   //按照传统方式获取数据 
   return; 
  } 
 
  //设置上传单个文件的大小的最大值,目前是设置为1024*1024字节,也就是1MB 
  //upload.setFileSizeMax(1024 * 1024); 
  //设置上传文件总量的最大值,最大值=同时上传的多个文件的大小的最大值的和,目前设置为10MB 
  upload.setSizeMax(1024 * 1024 * 10); 
 
  try { 
   //使用ServletFileUpload解析器解析上传数据,解析结果返回的是一个List<FileItem>集合,每一个FileItem对应一个Form表单的输入项 
   List<FileItem> items = upload.parseRequest(request); 
   Iterator<FileItem> iterator = items.iterator(); 
   while (iterator.hasNext()) { 
    FileItem item = iterator.next(); 
 
    //判断jsp提交过来的是不是文件 
    if (item.isFormField()) { 
     errorMessage = "请提交文件!"; 
     break; 
    } else { 
     //文件名 
     String fileName = item.getName(); 
     if (fileName == null || fileName.trim() == "") { 
      System.out.println("文件名为空!"); 
     } 
     //处理不同浏览器提交的文件名带路径问题 
     fileName = fileName.substring(fileName.lastIndexOf("\\") + 1); 
     //文件大小 
     Long fileSize = item.getSize(); 
     //文件扩展名 
     String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1); 
     //判断扩展名是否合法 
     if (!validExtension(fileExtension)) { 
      errorMessage = "上传文件非法!"; 
      item.delete(); 
      break; 
     } 
     //获得文件输入流 
     InputStream in = item.getInputStream(); 
     //得到保存文件的名称 
     String saveFileName = createFileName(fileName); 
     //得到文件保存路径 
     String realFilePath = createRealFilePath(savePath, saveFileName); 
     //创建文件输出流 
     FileOutputStream out = new FileOutputStream(realFilePath); 
     //创建缓冲区 
     byte buffer[] = new byte[1024]; 
     int len = 0; 
     while ((len = in.read(buffer)) > 0) { 
      //写文件 
      out.write(buffer, 0, len); 
     } 
     //关闭输入流 
     in.close(); 
     //关闭输出流 
     out.close(); 
     //删除临时文件 
     item.delete(); 
     <span style="color:#FF0000;">//将上传文件信息保存到附件表中 
     EntityAccessory entity = new EntityAccessory(); 
     entity.setFileName(fileName); 
     entity.setFileSize(fileSize); 
     entity.setFile_ext_name(fileExtension); 
     entity.setFilePath(realFilePath); 
     AccessoryDao.add(entity);</span> 
    } 
 
   } 
 
  } catch (FileUploadBase.FileSizeLimitExceededException e) { 
   e.printStackTrace(); 
   errorMessage = "单个文件超出最大值!!!"; 
  } catch (FileUploadBase.SizeLimitExceededException e) { 
   e.printStackTrace(); 
   errorMessage = "上传文件的总的大小超出限制的最大值!!!"; 
  } catch (FileUploadException e) { 
   e.printStackTrace(); 
   errorMessage = "文件上传失败!!!"; 
  } finally { 
   <span style="color:#FF0000;">if (!"".equals(errorMessage)) { 
    request.setAttribute("errorMessage", errorMessage); 
    request.getRequestDispatcher("pages/upload/error.jsp").forward(request, response); 
   } else { 
    response.sendRedirect("listUploadedFiles"); 
   }</span> 
 
  } 
 
 } 
 
 @Override 
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  doGet(request, response); 
 } 
 
 private boolean validExtension(String fileExtension) { 
  String[] exts = {"jpg", "txt", "doc", "pdf"}; 
  for (int i = 0; i < exts.length; i++) { 
   if (fileExtension.equals(exts[i])) { 
    return true; 
   } 
 
  } 
 
  return false; 
 } 
 
 private String createFileName(String fileName) { 
  return UUID.randomUUID().toString() + "_" + fileName; 
 } 
 
 /** 
  * 根据基本路径和文件名称生成真实文件路径,基本路径\\年\\月\\fileName 
  * 
  * @param basePath 
  * @param fileName 
  * @return 
  */ 
 private String createRealFilePath(String basePath, String fileName) { 
  Calendar today = Calendar.getInstance(); 
  String year = String.valueOf(today.get(Calendar.YEAR)); 
  String month = String.valueOf(today.get(Calendar.MONTH) + 1); 
 
 
  String upPath = basePath + File.separator + year + File.separator + month + File.separator; 
  File uploadFolder = new File(upPath); 
  if (!uploadFolder.exists()) { 
   uploadFolder.mkdirs(); 
  } 
 
  String realFilePath = upPath + fileName; 
 
  return realFilePath; 
 } 
} 

测试效果截图 



下集预告:实现附件删除功能!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持代码部落。

本文章来源于网络,作者是:indexman,由代码部落进行采编,如涉及侵权请联系删除!转载请注明出处:https://daimabuluo.cc/jspbiancheng/970.html

联系我们

在线咨询:点击这里给我发消息

邮件:dick@daimabuluo.cc

遇到问题?请给我们留言

请填写您的邮箱地址,我们将回复您的电子邮件