对于很多业务场景,可能涉及到对方系统传过来多个文件类型,但是其自身系统找不到对应的枚举类,像这种情况我们完全可以把对方传过来的在自身系统找不到对应文件类型归为“其它”类型,然后通过压缩流将这多个文件转化为压缩包文件,让客户自己去解压文件。
//将多个其它类型的退保附件下载打成压缩包
public static String urlZipdown(Map<String,String> tbfileMaps){
String docId="";
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
String noticeHttps = ConfigUtils.get("communication.protocol.type.jyjt");
try{
ZipOutputStream zipOut=new ZipOutputStream(outputStream);
// 用set集合防止重命名
Set<String> fileNameSet = Sets.newHashSet();
for(String key:tbfileMaps.keySet()){
String entryName = tbfileMaps.get(key);
// 如果有重复的名称, 则重命名
if (fileNameSet.contains(entryName)) {
entryName=entryName+ UUID.randomUUID().toString();
}
fileNameSet.add(entryName);
HttpGet httpget = new HttpGet(key);
// 加入Referer,防止防盗链
//httpget.setHeader("Referer", httpUrl);
HttpResponse response = httpClient.execute(httpget);
if(org.springframework.util.StringUtils.isEmpty(noticeHttps) || "HTTPS".equals(noticeHttps)){
System.setProperty("https.proxyHost", ConfigUtils.get("tbsp.jjyh.gateway.proxy.host"));
System.setProperty("https.proxyPort", ConfigUtils.get("tbsp.jjyh.gateway.proxy.port"));
}
byte[] byteArr = EntityUtils.toByteArray(response.getEntity());
zipOut.putNextEntry(new ZipEntry(entryName));
zipOut.write(byteArr);
}
zipOut.finish();
outputStream.flush();
byte[] arr = outputStream.toByteArray();
InputStream input = new ByteArrayInputStream(arr);
docId = TbspJresFileUtils.uploadFileWithOriginalName("zipFiles.zip",input);
input.close();
}catch (Exception e){
e.printStackTrace();
}
return docId;
}
原创文章,作者:wdmbts,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/269401.html