//将磁盘的多个文件打包成压缩包并输出流下载
@GetMapping(value = "/download1")public void download1(HttpServletRequest request, HttpServletResponse response) throwsException {
String outputFileName= "文件" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".zip";//设置response参数
response.reset();
response.setContentType("content-type:octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + new String((outputFileName).getBytes(), "iso-8859-1"));
ServletOutputStream out=response.getOutputStream();
ZipArchiveOutputStream zous= newZipArchiveOutputStream(out);
zous.setUseZip64(Zip64Mode.AsNeeded);
File f1= new File("D:\\testfile\\aaa.png");
File f2= new File("D:\\testfile\\bbb.png");
List fileList = new ArrayList<>();
fileList.add(f1);
fileList.add(f2);for(File file : fileList) {
String fileName=file.getName();
InputStream inputStream= newFileInputStream(file);
ByteArrayOutputStream baos= newByteArrayOutputStream();byte[] buffer = new byte[1024];intlen;while ((len = inputStream.read(buffer)) != -1) {
baos.write(buffer,0, len);
}if (baos != null) {
baos.flush();
}byte[] bytes =baos.toByteArray();//设置文件名
ArchiveEntry entry = newZipArchiveEntry(fileName);
zous.putArchiveEntry(entry);
zous.write(bytes);
zous.closeArchiveEntry();if (baos != null) {
baos.close();
}
}if(zous!=null) {
zous.close();
}
}//将网络url资源文件的多个文件打包成压缩包并输出流下载
@GetMapping(value = "/download2")public void download2(HttpServletRequest request, HttpServletResponse response) throwsException {
String outputFileName= "文件" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".zip";//设置response参数
response.reset();
response.setContentType("content-type:octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + new String((outputFileName).getBytes(), "iso-8859-1"));
ServletOutputStream out=response.getOutputStream();
ZipArchiveOutputStream zous= newZipArchiveOutputStream(out);
zous.setUseZip64(Zip64Mode.AsNeeded);
String path1= "http://www.baidu.com/img/bd_logo1.png";
String path2= "http://www.baidu.com/img/bd_logo1.png";
List pathList = new ArrayList<>();
pathList.add(path1);
pathList.add(path2);for(String path : pathList) {
String fileName= UUID.randomUUID() + ".png";
InputStream inputStream=getInputStreamFromUrl(path);
ByteArrayOutputStream baos= newByteArrayOutputStream();byte[] buffer = new byte[1024];intlen;while ((len = inputStream.read(buffer)) != -1) {
baos.write(buffer,0, len);
}if (baos != null) {
baos.flush();
}byte[] bytes =baos.toByteArray();//设置文件名
ArchiveEntry entry = newZipArchiveEntry(fileName);
zous.putArchiveEntry(entry);
zous.write(bytes);
zous.closeArchiveEntry();if (baos != null) {
baos.close();
}
}if(zous!=null) {
zous.close();
}
}/*** 经过网络地址获取文件InputStream
*
*@parampath 地址
*@return
*/
public staticInputStream getInputStreamFromUrl(String path) {
URL url= null;
InputStream is= null;try{
url= newURL(path);
}catch(MalformedURLException e) {
e.printStackTrace();
}try{
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.connect();
is=conn.getInputStream();
}catch(IOException e) {
e.printStackTrace();
}returnis;
}