Jsp+Servlet实现文件上传下载 删除上传文件(三)

所属分类: 网络编程 / JSP编程 阅读数: 1609
收藏 0 赞 0 分享

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

本章来实现一下删除已上传文件,同时优化了一下第一章中的代码。

废话少说,上代码得意

1.调整列表页面list.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> 
<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="<%=request.getContextPath()%>/removeUploadedFile?id=${acc.id}" rel="external nofollow" >删除</a></td> 
   </tr> 
  </c:forEach> 
 </c:if> 
</table> 
</body> 
</html> 

2.新增FileUtils工具类

package util; 
 
import java.io.File; 
 
/** 
 * 文件操作工具类 
 * 
 * @author xusucheng 
 * @create 2017-12-30 
 **/ 
public class FileUtils { 
 public static boolean delete(String path){ 
  File file = new File(path); 
  if(!file.isFile()){ 
   System.out.println("删除失败,文件:"+path+"不存在!"); 
   return false; 
  } 
 
  file.delete(); 
 
  return true; 
 } 
} 

3.调整附件实体DAO,新增load方法

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 EntityAccessory load(int id){ 
  Connection conn = DBUtil.getConnection(); 
  PreparedStatement ps=null; 
  ResultSet rs=null; 
  EntityAccessory entity = new EntityAccessory(); 
  String sql = "select id, file_name,file_size,file_ext_name,file_path from tbl_accessory where id=?"; 
  try { 
   ps = conn.prepareStatement(sql); 
   ps.setInt(1,id); 
   rs = ps.executeQuery(); 
   while (rs.next()){ 
    entity.setId(rs.getInt("id")); 
    entity.setFileName(rs.getString("file_name")); 
    entity.setFileSize(rs.getDouble("file_size")); 
    entity.setFile_ext_name(rs.getString("file_ext_name")); 
    entity.setFilePath(rs.getString("file_path")); 
   } 
  } catch (SQLException e) { 
   e.printStackTrace(); 
  }finally { 
   DBUtil.close(rs,ps,conn); 
  } 
 
  return entity; 
 } 
 
 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(); 
  } 
 } 
} 

4.新增删除文件处理器,removeUploadedFileServlet

package servlet.upload; 
 
import dao.upload.AccessoryDao; 
import entity.upload.EntityAccessory; 
import util.FileUtils; 
 
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; 
 
/** 
 * 删除已上传文件 
 * 
 * @author xusucheng 
 * @create 2017-12-30 
 **/ 
@WebServlet("/removeUploadedFile") 
public class removeUploadedFileServlet extends HttpServlet { 
 @Override 
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  //String filePath = request.getParameter("filePath"); 
  int fileId = Integer.valueOf(request.getParameter("id")); 
  EntityAccessory entity = AccessoryDao.load(fileId); 
  //删除文件 
  FileUtils.delete(entity.getFilePath()); 
  //删除数据库记录 
  AccessoryDao.remove(fileId); 
 
  //跳回到文件列表页 
  //request.getRequestDispatcher("listUploadedFiles").forward(request, response); 
  response.sendRedirect("listUploadedFiles"); 
 } 
 
 @Override 
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  doPost(request, response); 
 } 
 
 
} 

5.测试效果截图

删除前:

删除后:

6.下集预告

实现文件下载功能

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

更多精彩内容其他人还在看

spring动态bean注册示例分享

这篇文章主要介绍了spring动态bean注册示例,需要的朋友可以参考下
收藏 0 赞 0 分享

JSP判断移动设备的正则

天猫php判断移动设备的正则(个人猜测),觉得很好用,于是就决定移植到JSP里面,大家可以参考下
收藏 0 赞 0 分享

jsp文件绝对路径的设置方法

这篇文章主要介绍了jsp文件绝对路径的设置方法,需要的朋友可以参考下
收藏 0 赞 0 分享

jsp与sql语句的混合使用示例

这篇文章主要介绍了jsp与sql语句的混合使用,需要的朋友可以参考下
收藏 0 赞 0 分享

jsp获取action传来的session和session清空以及判断

这篇文章主要介绍了jsp获取action传来的session和session清空以及判断,需要的朋友可以参考下
收藏 0 赞 0 分享

jsp简单自定义标签的forEach遍历及转义字符示例

这篇文章主要介绍了jsp简单自定义标签的forEach遍历及转义字符,需要的朋友可以参考下
收藏 0 赞 0 分享

jsp自定义标签之ifelse与遍历自定义标签示例

这篇文章主要介绍了jsp自定义标签之ifelse与遍历自定义标签,需要的朋友可以参考下
收藏 0 赞 0 分享

JSP加载JS文件不起作用的有效解决方法

jsp导入jquery文件,老是不起作用,原因在于其不能访问/WEB-INF/目录下的文件,下面有个不错的解决方法,大家可以参考下
收藏 0 赞 0 分享

Jsp中如何让图片在div中居中

这篇文章主要介绍了Jsp中如何让图片在div中居中的小技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

使用jsp调用javabean实现超简单网页计算器示例

这篇文章主要介绍了使用jsp和javabean实现超简单网页计算器示例,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多