您的位置:首页 > 其它

原生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();
}

}


下载和上传使用非常广泛,希望大家能掌握住
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  servlet