文件上传与下载(二)smartupload
2015-06-09 21:58
465 查看
下载smartupload jar包
jsp前台页面
servlet上传页面
SmartUploadServlet.java
下载页面单个下载
SmartDownloadServlet.java
文件批量打包下载
BatchDownloadServlet.java
完整代码下载
jsp前台页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP '01.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <link rel="stylesheet" type="text/css" href="css/common.css" /> <script type="text/javascript" src="js/jquery-1.11.1.js"></script> <script type="text/javascript"> $(function(){ $(".thumbs a").click(function(){ var largePath = $(this).attr("href"); var largeAlt = $(this).attr("title"); $("#largeImg").attr({ src : largePath, alt : largeAlt }); return false; }); }); </script> </head> <body> <h2> 文件批量上传 </h2> <form action="smartUploadServlet.do" method="post" enctype="multipart/form-data"> 上传文件1:<input type="file" name="myfile1"><br> 上传文件2:<input type="file" name="myfile2"><br> 上传文件3:<input type="file" name="myfile3"><br> <input type="submit" value="提交">${result} </form> <hr> 下载:<a href="smartDownloadServlet.do?filename=img2-lg.jpg">img2-lg.jpg</a> <h2>文件批量下载</h2> <form action="batchDownloadServlet.do"> <input type="checkbox" name="filename" value="img2-lg.jpg">Image2 <input type="checkbox" name="filename" value="img3-lg.jpg">Image3 <input type="checkbox" name="filename" value="img4-lg.jpg">Image4 <input type="submit" value="下载"> </form> <hr> <h2>图片预览</h2> <p><img id="largeImg" src="images/img1-lg.jpg" alt="Large Image"/></p> <p class="thumbs"> <a href="images/img2-lg.jpg" title="Image2"><img src="images/img2-thumb.jpg"></a> <a href="images/img3-lg.jpg" title="Image3"><img src="images/img3-thumb.jpg"></a> <a href="images/img4-lg.jpg" title="Image4"><img src="images/img4-thumb.jpg"></a> <a href="images/img5-lg.jpg" title="Image5"><img src="images/img5-thumb.jpg"></a> <a href="images/img6-lg.jpg" title="Image6"><img src="images/img6-thumb.jpg"></a> </p> </body> </html>
servlet上传页面
SmartUploadServlet.java
package com.imooc.servlet; import java.io.File; import java.io.IOException; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jspsmart.upload.SmartUpload; import com.jspsmart.upload.SmartUploadException; public class SmartUploadServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //设置上传文件保存路径 String filePath = getServletContext().getRealPath("/") + "images"; File file = new File(filePath); if(!file.exists()){ file.mkdir(); } SmartUpload su = new SmartUpload(); //初始化对象 su.initialize(getServletConfig(), req, resp); //设置上传文件大小 su.setMaxFileSize(1024*1024*10); //设置所有文件的大小 su.setTotalMaxFileSize(1024*1024*100); //设置允许上传文件类型 su.setAllowedFilesList("txt,jpg,gif"); String result = "上传成功!"; //设置禁止上传的文件类型 try { su.setDeniedFilesList("rar,jsp,js"); //上传文件 su.upload(); int count = su.save(filePath); System.out.println("上传成功" + count + "个文件!"); } catch (Exception e) { result = "上传失败!"; if(e.getMessage().indexOf("1015") != -1){ result = "上传失败:上传文件类型不正确!"; }else if (e.getMessage().indexOf("1010") != -1){ result = "上传失败:上传文件类型不正确!"; }else if (e.getMessage().indexOf("1105") != -1){ result = "上传失败:上传文件大小大于允许上传的最大值!"; }else if (e.getMessage().indexOf("1110") != -1){ result = "上传失败:上传文件总大小大于允许上传总大小的最大值!"; } e.printStackTrace(); } req.setAttribute("result", result); req.getRequestDispatcher("jsp/02.jsp").forward(req, resp); } }
下载页面单个下载
SmartDownloadServlet.java
package com.imooc.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jspsmart.upload.SmartUpload; import com.jspsmart.upload.SmartUploadException; public class SmartDownloadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String filename = request.getParameter("filename"); SmartUpload su = new SmartUpload(); su.initialize(getServletConfig(), request, response); su.setContentDisposition(null); try { su.downloadFile("/images/"+ filename); } catch (SmartUploadException e) { e.printStackTrace(); } } }
文件批量打包下载
BatchDownloadServlet.java
package com.imooc.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jspsmart.upload.SmartUpload; import com.jspsmart.upload.SmartUploadException; public class SmartDownloadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String filename = request.getParameter("filename"); SmartUpload su = new SmartUpload(); su.initialize(getServletConfig(), request, response); su.setContentDisposition(null); try { su.downloadFile("/images/"+ filename); } catch (SmartUploadException e) { e.printStackTrace(); } } }
完整代码下载
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统