jsp檔案的簡單上傳與下載
阿新 • • 發佈:2018-12-05
@[JSP檔案的簡單上傳與下載]
- 一、前期工具準備:eclipse(這裡推薦eclipse,myeclipse也行,自己找了半天也找不到為什麼無法使用元件的問題),tomcat,還有jspsmartupload元件的jar包。
沒有元件可以去網上百度下載,或者加扣扣:1367844194問我要。 - 二、檔案上傳
1、upload.html
<!DOCTYPE html> <html> <head> <title>檔案上傳</title> <meta name="keywords" content="keyword1,keyword2,keyword3"> <meta name="description" content="this is my page"> <meta name="content-type" content="text/html; charset=GB2312"> </head> <body> <p align="center">上傳產品資訊</p> <form method="post" action="do_upload.jsp" enctype="multipart/form-data"> <table width="90%" border="1" align="center"> <tr> <td><div align="center">產品圖片: <input type="file" name="filel" size="30"> </div></td> </tr> <tr> <td><div align="center">產品說明: <input type="file" name="filel" size="30"> </div></td> </tr> <tr> <td><div align="center"> <input type="submit" name="submit" value="上傳"> </div></td> </tr> </table> </form> </body> </html>
2、do_upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page import="java.util.*,com.jspsmart.upload.*" %>//匯入上傳檔案所需元件的jar <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>檔案上傳處理頁面</title> <meta http-equiv="Content-Type" content="text/html;chatset=gb2312"> </head> <body> <% SmartUpload su = new SmartUpload();//建立SmartUpload物件 su.initialize(pageContext);//初始化頁面,pageContext是內建物件 //su.setMaxFileSize(10000); //su.setTotalMaxFileSize(20000); //su.setAllowedFilesList("doc,txt"); //su.setDeniedFilesList("exe,bat,jsp,htm,html,,"); su.upload(); int count = su.save("/upload",su.SAVE_VIRTUAL); /*注意:upload是路徑,此路徑必須是tomcat中的檔案路路徑 如果沒有要新建 ,新建在和web-inf同級資料夾中*/ out.println(count+"個檔案上傳成功!<br>"); for(int i=0;i<su.getFiles().getCount();i++)//遍歷檔案 { com.jspsmart.upload.File file = su.getFiles().getFile(i); if(file.isMissing()) continue; out.println("<br>檔名:"+file.getFileName()+"長度:"+file.getSize()); } %> </body> </html>
- 三、檔案下載
1、download.html
<!DOCTYPE html> <html> <head> <title>檔案下載</title> <http-equiv="content-type" content="text/html;charset=GB2312"> <meta name="keywords" content="keyword1,keyword2,keyword3"> <meta name="description" content="this is my page"> <meta name="content-type" content="text/html; charset=GB2312"> </head> <body> <p align="center">下載檔案頁面</p> <form action="do_download.jsp" method="post" enctype="multipart/form-data"> <table width="75%" border="1" align="center"> <tr> <td> <div align="center">單擊下載: <a href="upload/shop.docx">電子商城使用說明書</a> <input type="submit" name="download" value="下載"> </div> </td> </tr> </table> </form> </body> </html>
2、do_download.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.util.*,com.jspsmart.upload.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>檔案下載處理頁面</title>
<meta http-equiv="Content-Type" content="text/html;charset=GB2312">
</head>
<body>
<%
SmartUpload su = new SmartUpload();
su.initialize(pageContext);
su.setContentDisposition(null);
su.downloadFile("upload/shop.docx");
%>
</body>
</html>
程式碼重要核心都加了註釋!還有不懂得可以加我扣扣:1367844194,一起交流。