您的位置:首页 > 其它

远程文件批量下载并压缩ZIP包

2017-06-24 16:30 375 查看
一直苦苦追寻怎么批量下载网络图片,又不能一直for循环ajax请求,终于摸出来了,大概思路是,先单个文件下载到本地,然后压缩完,然后删除文件,再开始下载另一个文件,往复循环

@RequestMapping(value = "/downLoadZipFile")

public void downLoadZipFile(HttpServletResponse response,HttpServletRequest request) throws IOException{

List<Trip_Attachment> attachments=tripattachmentService.getByTripId(tripId);

Trip_Attachment使我们的业务实体ben,attUrl是我们需要导出的网络图片地址

//下载到本地路径,我下载的是D:\apache\apache-tomcat-8.5.13-windows-x64\apache-tomcat-8.5.13\webapps\ROOT\images

String realpath=request.getRealPath("/").replace("\\", "/")+"images/";

String imgName = "";

response.setContentType("APPLICATION/OCTET-STREAM");

response.setHeader("Content-Disposition","attachment; filename=myfile.zip");

ZipOutputStream out = new ZipOutputStream(response.getOutputStream());

try {

for(Iterator<Trip_Attachment> it = attachments.iterator();it.hasNext();){

Trip_Attachment file = it.next();

// imgName图片地址加图片名称,图片名称可以自己定义,我直接取得原图名称,如果去掉realpath,我试过了,也下载下来了,而且没有下载到本地

imgName=realpath+file.getAttUrl().substring(file.getAttUrl().lastIndexOf("/")+1,file.getAttUrl().length());

ZipUtils.doCompress(file.getAttUrl(),imgName, out);

response.flushBuffer();

}

} catch (Exception e) {

e.printStackTrace();

}finally{

out.close();

}

}

下面试工具类

package com.travelpay.util;

import java.io.ByteArrayOutputStream;

import java.io.DataInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.net.URL;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

/**

* 压缩文件工具类

*/

public class ZipUtils {

/**

* 压缩文件下载入口

* @param fileurl 文件链接如:http://img1.51travelpay.com/232/20170623/D3CCC8B3-D153-4265-9DC3-7D8675155F0A.jpg

* @param filename 新文件名路径:d:/a.jpg

* @param out ZipOutputStream

* @throws IOException

*/

public static void doCompress(String fileurl,String filename, ZipOutputStream out) throws IOException{

if(downloadPicture(fileurl,filename)){

File file=new File(filename);

if(doCompress(file,out)){

//压缩成功删除本地文件,如果filename没有指定路径,就是网络文件名,可以不执行删除

if(file.exists()){

file.delete();

}

}

}

}

public static boolean doCompress(File file, ZipOutputStream out) throws IOException{

if(file.exists()){

byte[] buffer = new byte[1024];

FileInputStream fis = new FileInputStream(file);

out.putNextEntry(new ZipEntry(file.getName()));

int len = 0 ;

// 读取文件的内容,打包到zip文件

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

out.write(buffer, 0, len);

}

out.flush();

out.closeEntry();

fis.close();

return true;

}else{

return false;

}

}

//链接url下载图片到本地

private static boolean downloadPicture(String urlList,String filename) {

URL url = null;

try {

url = new URL(urlList);

DataInputStream dataInputStream = new DataInputStream(url.openStream());

File file=new File(filename);

FileOutputStream fileOutputStream = new FileOutputStream(file);

ByteArrayOutputStream output = new ByteArrayOutputStream();

byte[] buffer = new byte[1024];

int length;

while ((length = dataInputStream.read(buffer)) > 0) {

output.write(buffer, 0, length);

}

fileOutputStream.write(output.toByteArray());

dataInputStream.close();

fileOutputStream.close();

return true;

} catch (Exception e) {

e.printStackTrace();

return false;

}

}

}

最终效果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: