远程文件批量下载并压缩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;
}
}
}
最终效果
@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;
}
}
}
最终效果
相关文章推荐
- python写的批量操作远程主机脚本(命令执行,上传、下载文件)
- 利用python fabric模块写的批量操作远程主机脚本(命令执行,上传、下载文件)
- php实现网站文件批量压缩下载功能
- Java 批量文件压缩导出并下载到本地示例代码
- java-flex 批量下载文件-压缩下载
- python写的批量操作远程主机脚本(命令执行,上传、下载文件)
- python写的批量操作远程主机脚本(命令执行,上传、下载文件)
- Jsp实现筛选并压缩文件批量下载
- 在ASP.NET中实现压缩多个文件为.zip文件,实现批量下载功能 (转载并优化处理篇)
- 批量下载,多文件压缩打包zip下载
- python写的批量操作远程主机脚本(命令执行,上传、下载文件)
- java下载远程http地址的图片文件到本地-自动处理图片是否经过服务器gzip压缩的问题
- python写的批量操作远程主机脚本(命令执行,上传、下载文件)
- 利用Node 搭配uglify-js压缩js文件,批量下载图片到本地
- strtus2 批量下载 中文问题、压缩文件等 ------ 讨论struts2工作流程
- python下paramiko模块学习之四:从远程主机批量下载文件到本机
- 在ASP.NET中实现压缩多个文件为.zip文件,实现批量下载功能
- 提供下载:批量图片压缩(生成缩略图或指定大小图片)及文件批量改名器
- PHP批量查询数据库下载远程文件脚本实例
- spring mvc 批量下载压缩文件夹中文件