JspSmartUpload 實現檔案上傳下載及Mysql實現分頁
阿新 • • 發佈:2018-12-14
一、寫在前面
這篇文章主要是分享使用JspSamrtUpload實現多檔案的上傳下載功能,及使用Mysql資料庫的 limit 函式實現檔案顯示列表的分頁顯示功能。
二、需要的jar包
下載後把它複製到web的lib目錄下即可。
三、關鍵程式碼
3.1 多檔案上傳前端程式碼 upload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" href="layui/css/layui.css"> <title>上傳下載檔案</title> <script type="text/javascript " src="js/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(e){ $("#button").click(function(e){ var comp = "<br><input type='file' name='upload'/><br>"; $("#files").append(comp); }); }); </script> <style type="text/css"> form{ margin:60px; } </style> </head> <body> <br><br> <form class="layui-form" action="UploadwithServlet" method="post" enctype="multipart/form-data"> <span id="files"> <input type="file" name="fileName" > <a href="ListServlet" class="layui-btn layui-btn-primary layui-btn-sm">下載頁面</a> <br> </span> <br> <input type="button" value="繼續新增" class="layui-btn layui-btn-primary layui-btn-sm" id="button" /> <button type="submit" class="layui-btn layui-btn-primary layui-btn-sm">確認上傳</button> </form><br> </body> </html>
多檔案上傳selvelt類 UploadwithServlet.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); // 1.實列化一個SmartUpload物件 SmartUpload su = new SmartUpload(); // 2.初始化SmartUpload物件 try { su.initialize(servletconfig, request, response); } catch (ServletException e1) { e1.printStackTrace(); } // 3.設定檔案上傳的限制 su.setAllowedFilesList("doc,docx,txt,pdf"); // 設定單個檔案的最大值 su.setMaxFileSize(3 * 1024 * 1024); // 設定檔案的總最大值 su.setTotalMaxFileSize(12 * 1024 * 1024); // 4.使用upload上傳 try { su.upload(); } catch (ServletException e2) { e2.printStackTrace(); } catch (SmartUploadException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } catch (IOException e2) { e2.printStackTrace(); } // 5.儲存檔案 Request reg = su.getRequest(); // 獲取上傳的全部檔案 Files files = su.getFiles(); // 獲得檔案的個數 int fileCount = files.getCount(); for (int i = 0; i < fileCount; i++) { File file = files.getFile(i); // 上傳檔案的時間 Date curDate = new Date(); long d = curDate.getTime(); // 獲取檔案的字尾名 String fileType = file.getFileExt(); // 檔案ID String fileId = String.valueOf(d); //檔名 String fileName = file.getFileName(); // 上傳檔案的客戶端地址 String uploadIp = request.getRemoteHost(); // 儲存的檔案地址 String fileUrl = "/upload/" + fileName ; // 獲得上傳時間 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 設定日期格式 String uploadDate = df.format(new Date()); // 獲取檔案大小 double fileSize = (double) file.getSize() / (1024 * 1024); try { //儲存檔案 file.saveAs(fileUrl); } catch (IOException e1) { e1.printStackTrace(); } catch (SmartUploadException e1) { e1.printStackTrace(); } /* * 以上程式碼就能儲存上傳的檔案到伺服器了 * 下面的這段程式碼是把檔案的資訊保持到資料庫 */ upload f = new upload(); f.setFileId(fileId); f.setFileName(fileName); f.setFileType(fileType); f.setFileSize(fileSize); f.setFileUrl(fileUrl); f.setUploadDate(uploadDate); f.setUploadIp(uploadIp); DaoFactory.getUploadDaoImpl().addUpload(f); } response.sendRedirect("ListServlet"); }
分頁列表前端程式碼
<div id = "btUpload"> <br> <a href="upload.jsp" class="layui-btn layui-btn-primary layui-btn-sm">上傳檔案</a> </div> <div id="table"> <table class="layui-table" > <thead> <tr> <th>檔名稱</th> <th>檔案型別</th> <th>檔案大小</th> <th>上傳使用者</th> <th>上傳時間</th> <th>操作</th> </tr> </thead> <tbody> <% //List<upload> list = DaoFactory.getUploadDaoImpl().listUpload(); List<upload> list = (List<upload>) request.getAttribute("list"); for(upload f:list) { %> <tr> <td><%=f.getFileName()%></td> <td><%=f.getFileType()%></td> <td><%=f.getFileSize()%>M</td> <td><%=f.getUploadIp()%></td> <td><%=f.getUploadDate()%></td> <td><a href="DownloadwithServlet?fileUrl=<%=f.getFileUrl()%>">下載</a></td> </tr> <% } %> </tbody> </table> </div> <center> <a href="ListServlet?action=first" class="layui-btn layui-btn-primary layui-btn-sm">首頁</a> <a href="ListServlet?action=P" class="layui-btn layui-btn-primary layui-btn-sm"><i class="layui-icon"></i></a> 第 <%=request.getAttribute("currentAge")%> 頁 共 <%=request.getAttribute("MaxPage")%> 頁 <a href="ListServlet?action=L" class="layui-btn layui-btn-primary layui-btn-sm"><i class="layui-icon"></i></a> <a href="ListServlet?action=last" class="layui-btn layui-btn-primary layui-btn-sm">尾頁</a> </center>