Jsp+Servlet實(shí)現(xiàn)文件上傳下載 刪除上傳文件(三)
接著上一篇講:Jsp+Servlet實(shí)現(xiàn)文件上傳下載(二)--文件列表展示
本章來(lái)實(shí)現(xiàn)一下刪除已上傳文件,同時(shí)優(yōu)化了一下第一章中的代碼。
廢話少說(shuō),上代碼得意
1.調(diào)整列表頁(yè)面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>文件大?。↘B)</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)開(kāi)啟了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ù)庫(kù)記錄 AccessoryDao.remove(fileId); //跳回到文件列表頁(yè) //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è)試效果截圖
刪除前:
刪除后:
6.下集預(yù)告
實(shí)現(xiàn)文件下載功能
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:JSP實(shí)現(xiàn)客戶信息管理系統(tǒng)
欄 目:JSP編程
下一篇:Jsp+Servlet實(shí)現(xiàn)文件上傳下載 文件列表展示(二)
本文標(biāo)題:Jsp+Servlet實(shí)現(xiàn)文件上傳下載 刪除上傳文件(三)
本文地址:http://mengdiqiu.com.cn/a1/JSPbiancheng/11432.html
您可能感興趣的文章
- 01-11web下載文件和跳轉(zhuǎn)的方法
- 01-11jsp文件下載功能實(shí)現(xiàn)代碼
- 01-11Spring獲取ApplicationContext對(duì)象工具類的實(shí)現(xiàn)方法
- 01-11web前端超出兩行用省略號(hào)表示的實(shí)現(xiàn)方法
- 01-11JSP servlet實(shí)現(xiàn)文件上傳下載和刪除
- 01-11jsp+servlet實(shí)現(xiàn)文件上傳與下載功能
- 01-11將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法
- 01-11秒殺系統(tǒng)Web層設(shè)計(jì)的實(shí)現(xiàn)方法
- 01-11Jsp+Servlet實(shí)現(xiàn)文件上傳下載 文件上傳(一)
- 01-11tomcat共享多個(gè)web應(yīng)用會(huì)話的實(shí)現(xiàn)方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-11web下載文件和跳轉(zhuǎn)的方法
- 01-11Spring注入Date類型的三種方法總結(jié)
- 01-11在JSP中使用formatNumber控制要顯示的小
- 01-11Properties 持久的屬性集的實(shí)例詳解
- 01-11EJB3.0部署消息驅(qū)動(dòng)Bean拋javax.naming.Na
- 01-11jsp文件下載功能實(shí)現(xiàn)代碼
- 01-11JSP頁(yè)面跳轉(zhuǎn)方法大全
- 01-11詳解Spring的核心機(jī)制依賴注入
- 01-11jsp 使用request為頁(yè)面添加靜態(tài)數(shù)據(jù)的實(shí)
- 01-11Spring獲取ApplicationContext對(duì)象工具類的
隨機(jī)閱讀
- 01-11Mac OSX 打開(kāi)原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10C#中split用法實(shí)例總結(jié)