国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

Jsp+Servlet實(shí)現(xiàn)文件上傳下載 刪除上傳文件(三)

2024-09-05 00:23:33
字體:
供稿:網(wǎng)友

接著上一篇講:Jsp+Servlet實(shí)現(xiàn)文件上傳下載(二)--文件列表展示

本章來實(shí)現(xiàn)一下刪除已上傳文件,同時(shí)優(yōu)化了一下第一章中的代碼。

廢話少說,上代碼得意

1.調(diào)整列表頁面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.調(diào)整附件實(shí)體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默認(rèn)開啟了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());   //刪除數(shù)據(jù)庫記錄   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.測(cè)試效果截圖

刪除前:

Jsp,Servlet,上傳,下載

刪除后:

Jsp,Servlet,上傳,下載

6.下集預(yù)告

實(shí)現(xiàn)文件下載功能

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JSP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 冷水江市| 巴里| 浏阳市| 彰武县| 阿拉善盟| 普兰县| 博罗县| 逊克县| 西华县| 泸西县| 防城港市| 胶南市| 桂阳县| 当阳市| 贵溪市| 临海市| 犍为县| 栾城县| 莫力| 米脂县| 高陵县| 文化| 扶沟县| 孟村| 邹城市| 临颍县| 张家港市| 阿坝县| 昭苏县| 土默特左旗| 盐池县| 苏尼特左旗| 秭归县| 伊金霍洛旗| 榆树市| 海阳市| 陇川县| 丹凤县| 凉山| 尖扎县| 石河子市|