欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-28 22:48:37
接着上一篇讲:jsp+servlet实现文件上传下载(二)--文件列表展示 本章来实现一下删除已上传文件,同时优化了一下第一章中的代码。 废话少说,上代码得意...

接着上一篇讲: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.测试效果截图

删除前:

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

删除后:

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

6.下集预告

实现文件下载功能

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