java 实现多文件压缩导出操作

[size=large]1 controller [/size]

@RequestMapping(value = "/{project}/{moduel}/export/product/{productId}", method = RequestMethod.GET)
public void exportProductData(HttpServletRequest request, HttpServletResponse response,
@PathVariable("project") String project, @PathVariable("moduel") String moduel,
@PathVariable("productId") String productId) throws IOException {
String baseUrl = String.format("/%s/%s/%s", project, moduel, "export/product");
LOG.info(String.format("正在接收%s请求", baseUrl));

Map<String, Object> reqMap = new HashMap<String, Object>();
reqMap.put("productId", productId);
RtResponse rtResponse = reqUrlDefService.getResponse(baseUrl, request, reqMap);
Map<String,Object> data = (Map<String, Object>) rtResponse.getData();

Map<String,Map<String,Object>> bytes = (Map<String,Map<String,Object>>) data.get("files");

List<File> files = new ArrayList<File>();

for(Entry<String, Map<String, Object>> map : bytes.entrySet()){
Map<String,Object> byteMap = map.getValue();
String fileName = (String) byteMap.get("fileName");
String contentType = (String) byteMap.get("contentType");
byte[] byt = (byte[]) byteMap.get("fileByte");
File file = new File(fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(byt);
files.add(file);
fos.close();
}

String filename = URLEncoder.encode("product-struct.zip", "UTF-8");
byte[] buf = new byte[1024];
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(filename));
out.setMethod(ZipOutputStream.DEFLATED);
for (int i = 0; i < files.size(); i++) {
FileInputStream in = new FileInputStream(files.get(i));
out.putNextEntry(new ZipEntry(files.get(i).getName()));
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
out.close();

int bytesRead = 0;
byte[] buffer = new byte[8092];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
InputStream is = new FileInputStream(filename);
while ((bytesRead = is.read(buffer, 0, 8092)) != -1) {
bos.write(buffer, 0, bytesRead);
}

response.reset();
response.setHeader("Content-disposition", "attachment;filename=" + filename);
response.setContentType("application/x-zip-compressed");

OutputStream os = response.getOutputStream();
os.write(bos.toByteArray());
os.flush();
os.close();


}


[size=large]2 service[/size]

@Override
public RtResponse exportProductInfo(RtRequest request) throws Exception {
RtResponse response = new RtResponse();
Integer code = ResultCode.FAILURE;

String dataJson = "";

Map<String,Map<String,Object>> bytes = new HashMap<String,Map<String,Object>>();
//产品图片
if(AppUtils.isNotBlank(adjuncts)){
for(ProductAdjunct pa : adjuncts){
String fileId = pa.getAdjunctPath();
GridFS gridFS = new GridFS(mongotemplate.getDb(), "file_collections");
DBObject query = new BasicDBObject("_id", fileId);
GridFSDBFile gridFSDBFile = gridFS.findOne(query);

logger.info("附件接口 文件流内容...{}", gridFSDBFile.getInputStream());
Map<String, Object> maps = new HashMap<String, Object>();
maps.put("fileName", pa.getAdjunctClass()+"-+"+gridFSDBFile.getFilename());
maps.put("contentType", gridFSDBFile.getContentType());
maps.put("fileByte", FileUtils.input2byte(gridFSDBFile.getInputStream()));
maps.put("type", pa.getAdjunctClass());
bytes.put(pa.getAdjunctClass(),maps);
}
}

dataJson = JsonUtil.javaObjToJson(productDataObject);
Map<String, Object> maps = new HashMap<String, Object>();
maps.put("fileName", "ProductData.txt");
maps.put("contentType", "application/txt");
maps.put("fileByte", dataJson.getBytes("UTF-8"));
maps.put("type", "product");
bytes.put("product",maps);

data.put("files",bytes);
code = ResultCode.SUCCESS;
response.setCode(code);
response.setData(data);

return response;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

annan211

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值