创建文件夹 下载远程oss图片到本地
打压缩包
java hutool 导出压缩包
public void compressedPackage(
@RequestParam(name = "num", required = true) Integer num,
HttpServletRequest req,
HttpServletResponse response
) throws IOException {
long startTime = System.currentTimeMillis();
log.info("==========导出开始===========");
数据查询
log.info("==========导出查询耗时"+ (System.currentTimeMillis() - startTime)+"===========");
//创建目录
String format = DateUtil.format(new Date(), "yyyyMMddHHmmss");
String mulu = "c:/compressed/img/" + format;
String muluZip = "c:/compressed/zip/" + format;
FileUtil.mkdir(mulu);
FileUtil.mkdir(muluZip);
CxkjUtil cxkjUtil=new CxkjUtil();
for (CxWifiCode cxWifiCode : cxWifiCodes) {
if (oConvertUtils.isNotEmpty(cxWifiCode.getImg())) {
String url1 = CxkjUtil.concatUrlPrefixString(cxWifiCode.getImg());
//底图
String baImg = cxWifiBackground.getImg();
//计算文字位置
int a = -5;
int length = String.valueOf(cxWifiCode.getCode()).length();
int floor = (int) Math.floor(length / 2);
// 文件名
String suffix1 = url1.substring(url1.lastIndexOf("."));
String mu=mulu + "/" + cxWifiCode.getCode() + suffix1;
//拼接保存
String bais = cxkjUtil.getQrCodeUrl(baImg,
url1, 0, 220,
String.valueOf(cxWifiCode.getCode()), 3, floor * a, 630,mu);
//取编号
// long file = HttpUtil.downloadFile(bais, mulu + "/" + cxWifiCode.getCode() + suffix1);
}
}
log.info("==========导出下载合并耗时"+ (System.currentTimeMillis() - startTime)+"===========");
String zip = muluZip + "/" + format + ".zip";
ZipUtil.zip(mulu, zip);
log.info("==========导出压缩耗时"+ (System.currentTimeMillis() - startTime)+"===========");
OutputStream out = null;
InputStream fis = null;
// 创建zip文件输出流
// FileOutputStream fos = new FileOutputStream(zipFileStr);
// 将打包后的文件写到客户端,输出的方法同上,使用缓冲流输出
fis = new BufferedInputStream(new FileInputStream(zip));
// response.reset(); // 必要地清除response中的缓存信息
response.setContentType("application/octet-stream");
// response.setContentType("text/html; charset=utf-8"); // 设置编码字符
String ymd = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
// response.setHeader("Content-disposition", "attachment;filename=" + new String("二维码".getBytes("GB2312"), "ISO8859-1")
// + "" + ymd + ".zip");// 设置下载的文件名称
out = response.getOutputStream();
byte[] buff = new byte[4096];
int size = 0;
while ((size = fis.read(buff)) != -1) {
out.write(buff, 0, size);
}
out.flush();
out.close();
}
正文完