批量图片打包zip下载
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test")
public class Test {
@RequestMapping(value="downLoadFiles")
public static HttpServletResponse downLoadFiles(
HttpServletRequest request, HttpServletResponse response,String imgurl,String userName)
throws Exception {
if(userName==null || userName.equals("")){
userName="影像资料";
}
try {
String[] arr=imgurl.split(",");
List list=new ArrayList();
list = Arrays.asList(arr);
/**这个集合就是你想要打包的所有文件,
* 这里假设已经准备好了所要打包的文件*/
List<URL> files = new ArrayList<URL>();
if(list.size()>0){
for(int i=0;i<list.size();i++){
URL file=new URL(list.get(i).toString());
files.add(file);
}
}
/**创建一个临时压缩文件,
* 我们会把文件流全部注入到这个文件中
* 这里的文件你可以自定义是.rar还是.zip*/
File file = new File(userName+".zip");
if (!file.exists()){
file.createNewFile();
}
response.reset();
//response.getWriter()
//创建文件输出流
FileOutputStream fous = new FileOutputStream(file);
/**打包的方法我们会用到ZipOutputStream这样一个输出流,
* 所以这里我们把输出流转换一下*/
ZipOutputStream zipOut
= new ZipOutputStream(fous);
/**这个方法接受的就是一个所要打包文件的集合,
* 还有一个ZipOutputStream*/
zipFile(files, zipOut);
zipOut.close();
fous.close();
return downloadZip(file,response);
}catch (Exception e) {
e.printStackTrace();
}
return response ;
}
/**
* 把接受的全部文件打成压缩包
* @param List<File>;
* @param org.apache.tools.zip.ZipOutputStream
*/
public static void zipFile
(List files,ZipOutputStream outputStream) {
int size = files.size();
for(int i = 0; i < size; i++) {
URL file = (URL) files.get(i);
zipFile(file, outputStream);
}
}
public static HttpServletResponse downloadZip(File file,HttpServletResponse response) {
try {
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
//如果输出的是中文名的文件,在此处就要用URLEncoder.encode方法进行处理
response.setHeader("Content-Disposition", "attachment;filename=" +URLEncoder.encode(file.getName(), "UTF-8"));
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}finally{
try {
File f = new File(file.getPath());
f.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
return response;
}
/**
* 根据输入的文件与输出流对文件进行打包
* @param File
* @param org.apache.tools.zip.ZipOutputStream
*/
public static void zipFile(URL inputFile,
ZipOutputStream ouputStream) {
try {
URL url = new URL(inputFile.toString());
/*此为联系获得网络资源的固定格式用法,以便后面的in变量获得url截取网络资源的输入流*/
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
DataInputStream in = new DataInputStream(connection.getInputStream());
BufferedInputStream bins = new BufferedInputStream(in, 512);
//org.apache.tools.zip.ZipEntry
ZipEntry entry = new ZipEntry(inputFile.toString().substring(inputFile.toString().length()-20));
ouputStream.putNextEntry(entry);
// 向压缩文件中输出数据
int nNumber;
byte[] buffer = new byte[512];
while ((nNumber = bins.read(buffer)) != -1) {
ouputStream.write(buffer, 0, nNumber);
}
// 关闭创建的流对象
bins.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
来源:oschina
链接:https://my.oschina.net/u/2452380/blog/774600