jsp页面js代码:

function downloadAttached(){
var id = [];
id.push(infoid);
var options = {};
options.action = "${pageContext.request.contextPath}/DocumentController/downloadattached";
options.argname1="id";
options.argvalue1=id.join(',');
filedownload(options);
parent.$.messager.progress('close');
}

控制层java相关代码

public ModelAndView DownloadAttached(HttpServletRequest request, HttpServletResponse response,String id,String ie) throws Exception{
String toClient=attService.downloadAttached(request, response, id, projectName);
String[] split = toClient.split("/");
String storeName = toClient;
String realName = split[split.length - 1];
String contentType = "application/x-zip-compressed";

try {
excelExport.download(request,response,storeName,contentType,realName,ie);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}

//download方法

public static void download(HttpServletRequest request,
HttpServletResponse response, String storeName, String contentType,
String realName, String ie) throws Exception {

response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;

String ctxPath = request.getSession().getServletContext()
.getRealPath("/");
String downLoadPath = ctxPath + storeName;

long fileLength = new File(downLoadPath).length();

response.setContentType(contentType);
String finalFileName;

if (ie.equals("1")) {
finalFileName = java.net.URLEncoder.encode(realName, "UTF8");
} else {
finalFileName = new String(realName.getBytes("UTF-8"), "ISO-8859-1");
}

response.setHeader("Content-disposition", "attachment; filename="
+ finalFileName);
response.setHeader("Content-Length", String.valueOf(fileLength));

bis = new BufferedInputStream(new FileInputStream(downLoadPath));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bis.close();
bos.close();

}

打包下载代码:将压缩包文件下载到服务器项目中的目录下然后用户去下载

private static String downpath = "down/";
public static String compress(String[] path,String strZipName) throws Exception {
byte[] buffer = new byte[1024];
String zippath = PathUtil.combine(PathUtil.getWebRootPath(), downpath);

//strZipName中有空格会生成、下载不成功
String zipName=zippath+strZipName+".zip";
String returnzipname=downpath+strZipName+".zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipName));

File[] file1= new File[path.length];

for(int i=0;i<file1.length;i++) {
file1[i]=new File(path[i]);
String combine = PathUtil.combine(PathUtil.getWebRootPath(),
file1[i].toString());
FileInputStream fis = new FileInputStream(combine);

out.putNextEntry(new ZipEntry(file1[i].getName()));
int len;

while((len = fis.read(buffer))>0) {

out.write(buffer,0,len);

}

out.closeEntry();

fis.close();

}

out.close();
return returnzipname;
}

java 实现多文件打包下载的更多相关文章

  1. PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载 && Linux下的ZipArchive配置开启压缩 &&搞个鸡巴毛,写少了个‘/’号,浪费了一天

    PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,具体开启方法就不说了,不同的平台开启PHP扩增的方法网上都有,如有 ...

  2. PHP 多文件打包下载 zip

    <?php $zipname = './photo.zip'; //服务器根目录下有文件夹public,其中包含三个文件img1.jpg, img2.jpg, img3.jpg,将这三个文件打包 ...

  3. Java 多个文件压缩下载

    有时候会有多个附件一起下载的需求,这个时候最好就是打包下载了 首先下面这段代码是正常的单个下载 public void Download(@RequestParam("file_path&q ...

  4. flask BytesIO() 多个文件打包下载 zipfile

    使用zipfile模块可以将多个文件打包成zip文件进行下载,但是常规的操作方式会在服务器磁盘上生成一个zip文件占用磁盘空间. 后引入BytesIO将文件写入到内存中然后下载: def dl_pla ...

  5. servlet实现多文件打包下载

    当用户一次下载多个文件时.普通情况是,每下载一个文件,均要弹出一个下载的对话框.这给用户造成了非常大不便. 比較理想的情况是,用户选择多个文件后.server后端直接将多个文件打包为zip.以下贴出实 ...

  6. java 多个文件打包zip

    /** * 多个文件打包成zip */ public class ZipDemo { private static void create() throws Exception{ String pat ...

  7. Java批量文件打包下载

    经常遇到选择多个文件进行批量下载的情况,可以先将选择的所有的文件生成一个zip文件,然后再下载,该zip文件,即可实现批量下载,但是在打包过程中,常常也会出现下载过来的zip文件中里面有乱码的文件名, ...

  8. Java批量文件打包下载zip

    网上看了很多,本文使用ant.jar中的org.apache.tools.zip,页面用js表单提交 代码供参考: ACTION: /* * 另存为 */ @RequestMapping(" ...

  9. 【Java】Java批量文件打包下载zip

    网上看了很多,本文使用ant.jar中的org.apache.tools.zip,页面用js表单提交 代码供参考: ACTION: /*      * 另存为      */     @Request ...

随机推荐

  1. Micropython TurnipBit 青少年入门编程 交通灯实验

    不知道大家小时候对红绿灯的原理有什么研究过,我是农村的孩子直到初中才见到真实的红绿灯,当时我记得很清楚,在那个路口站了五六分钟就盯着红绿灯变换,搞不清原理,只觉得神奇.现在想来实在可笑,今天写这个的很 ...

  2. 24.Django路由规则

    路由规则 1.基于正则的url 在templates目录下创建index.html.detail.html文件 (1)index.html <!DOCTYPE html> <html ...

  3. epoll 实现回射服务器

    epoll是I/O复用模型中相对epoll和select更高效的实现对套接字管理的函数. epoll有两种模式 LT 和 ET 二者的差异在于 level-trigger 模式下只要某个 socket ...

  4. ccd采集

  5. C++中 #include<>与#include""

    #include<> 使用尖括号表示在包含文件目录中去查找(包含目录是由用户在设置环境时设置的),而不在源文件目录去查找: #include"" 使用双引号则表示首先在 ...

  6. 所使用的“EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”版本高于所引用的程序集“EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”的版本

    错误信息:所使用的"EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&qu ...

  7. sublime COMMAND + B 调用 python3 运行

    用sublime写了python3的代码,COMMAND + B运行调用 PYTHON3 我们先来新建一个sublime build system 然后自动打开了一个文本,清空并写入以下内容: { & ...

  8. 新装的Linux服务系统安装MySQL

    目的描述:全新的腾讯云Linux服务器,系统是ubuntu 16.04.需要在上面安装mysql数据库. 使用XShell远程登录,在终端窗口中使用sudo apt-get 指令在线安装mysql. ...

  9. 简单的nodejs 文件系统(fs)读写例子。

    在nodejs中,可以通过fs(file system)模块进行文件的I/O操作. API链接地址: http://nodeapi.ucdok.com/#/api/fs.html 下面进行fs文件系统 ...

  10. 希尔排序(shell‘ sort)

    希尔排序是1959 年由D.L.Shell 提出来的,相对直接排序有较大的改进.希尔排序又叫缩小增量排序 基本思想: 先将整个待排序的记录序列分割成为若干子序列分别进行直接插入排序,待整个序列中的记录 ...