原生servlet配合smartupload实现批量下载和批量上传
2016-05-31 21:10
344 查看
废话不多说,直接贴代吗
下载和上传使用非常广泛,希望大家能掌握住
页面代码:
<%@ 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 'index.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="styles.css"> --> </head> <body> ${msg } 上传 <form action="<%=basePath %>/SmartServlet" method="post" enctype="multipart/form-data"> <input type="file" name="file1"> <input type="file" name="file2"> <input type="file" name="file3"> <input type="file" name="file4"> <input type="submit" value="上传"> </form> <hr/> 批量下载 <form action="<%=basePath%>/SmartDownServlet" method="get"> <input type="checkbox" name="filename" value="img1.jpg">Image2 <input type="checkbox" name="filename" value="img2.jpg">Image3 <input type="checkbox" name="filename" value="img3.jpg">Image4 <input type="submit" value="下载"> </form> </body> </html>
批量上传,servlet代码,需要注意的是需要将表单设置为enctype=”multipart/form-data”,这样就会以二进制流的形式传输表单
package com.leige.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jspsmart.upload.SmartUpload; public class SmartServlet 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 path=this.getServletContext().getRealPath("/img"); System.out.println(path); SmartUpload smartUpload=new SmartUpload(); //初始化对象 smartUpload.initialize(getServletConfig(), request, response); //设置上传文件大小 smartUpload.setMaxFileSize(1024*1024*10); //设置所有文件的大小 smartUpload.setTotalMaxFileSize(1024*1024*100); //设置允许上传文件类型 smartUpload.setAllowedFilesList("txt,jpg,gif,png"); String result = "上传成功!"; try { //设置禁止上传的文件类型 smartUpload.setDeniedFilesList("rar,jsp,js"); //上传文件 smartUpload.upload(); int count = smartUpload.save(path); System.out.println("上传成功" + count + "个文件!"); } catch (Exception e) { //smartupload定义的一些错误代码,挑出常用的 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(); } request.setAttribute("msg", result); request.getRequestDispatcher("/index.jsp").forward(request, response); } }
批量下载,需要注意的是,当用户选择批量下载时,我们不可能每个文件都弹出一个下载框,这样在体验上也不好,而且大量数据不经过压缩,就传输,也是对带宽的浪费,所以好的解决办法,压缩成包:
package com.leige.servlet; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; import com.jspsmart.upload.Files; import com.jspsmart.upload.SmartUpload; import com.jspsmart.upload.SmartUploadException; public class SmartDownServlet 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 { //设置请求头信息 response.setContentType("application/x-msdownload"); response.setHeader("Content-Disposition", "attachment;filename=test.zip"); String path = getServletContext().getRealPath("/") + "img/"; //初始化smartupload SmartUpload sm=new SmartUpload(); sm.initialize(getServletConfig(), request, response); sm.setContentDisposition(null); String[] fileNames=request.getParameterValues("filename"); String str = ""; String rt = "\r\n"; //返回一个压缩包 ZipOutputStream zos = new ZipOutputStream(response.getOutputStream()); for(String filename : fileNames){ str += filename + rt; File file = new File(path + filename); zos.putNextEntry(new ZipEntry(filename)); FileInputStream fis = new FileInputStream(file); //复制文件到压缩流中 IOUtils.copy(fis, zos); zos.flush(); fis.close(); } //设置注释 zos.setComment("下载成功:" + rt + str); zos.flush(); zos.close(); } }
下载和上传使用非常广泛,希望大家能掌握住
相关文章推荐
- Java Servlet生成JSON格式数据并用jQuery显示的方法
- jsp和servlet中实现页面跳转的方式实例总结
- java servlet结合mysql搭建java web开发环境
- Servlet与JSP间的两种传值情况
- JSP、Servlet中get请求和post请求的区别总结
- jsp和servlet的区别探讨
- Struts2访问servlet分享
- java中Servlet处理乱码的方法
- Servlet 过滤器详细介绍
- JSP+Servlet+JavaBean实现登录网页实例详解
- servlet中session简介和使用例子
- 在Jsp Servlet中页面重新定向总汇
- JSP使用Servlet作为控制器实现MVC模式实例详解
- JSP使用Servlet过滤器进行身份验证的方法
- Servlet动态网页技术详解
- jsp和servlet操作mysql中文乱码问题的解决办法
- 基于jsp+servlet实现的简单博客系统实例(附源码)
- js调用后台servlet方法实例
- 利用JQuery和Servlet实现跨域提交请求示例分享