/**
* 可用HTTP地址获取图片二进制 输出
*/
@RequestMapping(value = "/test")
@ResponseBody
public void test(HttpServletRequest request, HttpServletResponse response) {
String t = "http://127.0.0.1:39090/oa/orderContractFile/202001/553f0479-6e8e-4ce0-a1b5-fbee8bf0fb4b.png";
//生成打包下载后的zip文件:Papers.zip
String papersZipName = "Papers.zip";
//zip文件保存路径
String zipPath = "d:/" + papersZipName;
try {
@Cleanup
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipPath));
int j = 3;
for (int i = 0; i < 2; i++) {
//获得下载文件完整路径
String downloadPath = t;
@Cleanup
FileInputStream fis = new FileInputStream("d:/test.png");
out.putNextEntry(new ZipEntry("测试" + ++j + ".png"));
//写入压缩包
HttpResponse httpResponse = HttpRequest.get(t)
.execute();
byte[] bytes = httpResponse.bodyBytes();
out.write(bytes);
}
} catch (Exception e) {
e.printStackTrace();
}
try {
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/zip;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=Papers.zip");
//开始下载
@Cleanup
BufferedInputStream is = new BufferedInputStream(new FileInputStream(new File(zipPath)));
@Cleanup
BufferedOutputStream outt = new BufferedOutputStream(response.getOutputStream());
// deviceCamera/test
byte[] buff = new byte[1024];
int lenn = 0;
while ((lenn = is.read(buff, 0, buff.length)) != -1) {
outt.write(buff, 0, lenn);
}
} catch (Exception e) {
e.printStackTrace();
}
File file = new File(zipPath);
file.delete();
}
来源:CSDN
作者:诚实可靠的悟空
链接:https://blog.csdn.net/qq_20519453/article/details/104493081