/**
* 根据url打包下载
* @param response
* @throws IOException
*/
public void downloadZipFile(HttpServletResponse response) {
try {
String filename ="文件名";
response.setContentType(MediaType.APPLICATION_OCTET_STREAM.toString());
response.setHeader("Content-Disposition", "attachment; filename=" + filename + ".zip");
//查询报关单号的全部附件
ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());
for (BillFileViewVo billFileViewVo : billFileVo.getBillFileViews()) {
ZipEntry zipEntry = new ZipEntry(billFileViewVo.getOriginfileName());
zipOutputStream.putNextEntry(zipEntry);
// 构造URL
URL url = new URL(billFileViewVo.getAliyunFileUrl());
// 打开连接
URLConnection con = url.openConnection();
//设置请求超时为10s
con.setConnectTimeout(10 * 1000);
// 输入流
InputStream is = con.getInputStream();
IOUtils.copy(is, zipOutputStream);
is.close();
}
zipOutputStream.closeEntry();
zipOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 根据模板生成文件打包下载
*
* @param response
* @throws IOException
*/
public void downloadZipFile(HttpServletResponse response) throws IOException {
try {
String filename = "文件名";
response.setContentType(MediaType.APPLICATION_OCTET_STREAM.toString());
response.setHeader("Content-Disposition", "attachment; filename=" + new String(filename.getBytes("gb2312"), "ISO8859-1") + ".zip");
//查询报关单号的全部附件
ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());
for (int i = 0; i < 2; i++) {
TemplateExportParams params = new TemplateExportParams("templates/12仓.xlsx", true);
Map<String, Object> map = new HashMap<>();
//往map添加你要生成的数据
Workbook wb = ExcelExportUtil.exportExcel(params, map);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ByteArrayInputStream is = null;
try {
try {
wb.write(os);
} catch (IOException var21) {
var21.printStackTrace();
}
byte[] content = os.toByteArray();
is = new ByteArrayInputStream(content);
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
InputStream in = is;
ZipEntry zipEntry = new ZipEntry(i + ".xlsx");
zipOutputStream.putNextEntry(zipEntry);
// 输入流
IOUtils.copy(in, zipOutputStream);
in.close();
}
zipOutputStream.closeEntry();
zipOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
java实现文件打包下载
最新推荐文章于 2024-08-07 16:54:05 发布