public void exportBatch(HttpServletResponse response,HttpServletRequest request,List<String> urls) {
try {
UrlFilesToZip s = new UrlFilesToZip();
List<ByteFileBean> fileBeans = new ArrayList<>();
for (int i = 0; i < urls.size ; i++) {
byte[] bytes = s.getImageFromURL(url);
ByteFileBean byteFileBean = new ByteFileBean();
byteFileBean.setFileName("文件夹" + i + File.separator+s.getFileName(url);
byteFileBean.setFileContent(bytes);
fileBeans.add(byteFileBean);
}
String filename = s.encodeChineseDownloadFileName(request,"xxx.zip");
s.downLoadFilesToZip(fileBeans, filename, request, response, response.getOutputStream());
} catch (FileNotFoundException ex) {
log.error("FileNotFoundException", ex);
throw new GenericException(BusCodeEnum.BATCH_DOWN_LOAD_FILE_FAIL.getCode(),BusCodeEnum.BATCH_DOWN_LOAD_FILE_FAIL.getMsg());
} catch (Exception ex) {
log.error("Exception", ex);
throw new GenericException(BusCodeEnum.BATCH_DOWN_LOAD_FILE_FAIL.getCode(),BusCodeEnum.BATCH_DOWN_LOAD_FILE_FAIL.getMsg());
}
}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@SuppressFBWarnings
public class ByteFileBean {
private String fileName;
private byte[] fileContent;
}
public class UrlFilesToZip {
public byte[] getImageFromURL(String urlPath) {
byte[] data = null;
InputStream is = null;
HttpURLConnection conn = null;
try {
URL url = new URL(urlPath);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setRequestMethod("GET");
conn.setConnectTimeout(6000);
is = conn.getInputStream();
if (conn.getResponseCode() == 200) {
data = readInputStream(is);
} else {
data = null;
}
} catch (MalformedURLException e) {
log.error("MalformedURLException", e);
} catch (IOException e) {
log.error("IOException", e);
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
log.error("IOException", e);
}
assert conn != null;
conn.disconnect();
}
return data;
}
public byte[] readInputStream(InputStream is) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length = -1;
try {
while ((length = is.read(buffer)) != -1) {
baos.write(buffer, 0, length);
}
baos.flush();
} catch (IOException e) {
log.error("IOException", e);
}
byte[] data = baos.toByteArray();
try {
is.close();
baos.close();
} catch (IOException e) {
log.error("IOException", e);
}
return data;
}
public String getFileName(String url){
return url.substring(url.lastIndexOf('/') + 1);
}
public String encodeChineseDownloadFileName(HttpServletRequest request, String fileName) throws Exception {
String newFileName = null;
String agent = request.getHeader("USER-AGENT");
if (null != agent) {
if (-1 != agent.indexOf("Firefox")) {
newFileName =
"=?UTF-8?B?" + (new String(org.apache.commons.codec.binary.Base64.encodeBase64(fileName.getBytes(
"UTF-8")))) + "?=";
} else if (-1 != agent.indexOf("Chrome")) {
newFileName = new String(fileName.getBytes(), "ISO8859-1");
} else {
newFileName = java.net.URLEncoder.encode(fileName, "UTF-8");
newFileName = newFileName.replace("+", "%20");
}
} else {
newFileName = fileName;
}
return newFileName;
}
public void downLoadFilesToZip(List<ByteFileBean> fileBeans, String zipName,
HttpServletRequest request, HttpServletResponse response,
OutputStream outputStream) throws GenericException {
if (fileBeans == null || fileBeans.isEmpty()) {
throw new GenericException(BusCodeEnum.DOWN_LOAD_FILES_IS_NULL.getCode(),
BusCodeEnum.DOWN_LOAD_FILES_IS_NULL.getMsg());
}
if (com.catxcloud.core.util.StringUtils.isBlank(zipName)) {
throw new GenericException(BusCodeEnum.ZIP_NAME_IS_NULL.getCode(),
BusCodeEnum.ZIP_NAME_IS_NULL.getMsg());
}
String zipNameAlias;
try {
zipNameAlias = zipName.toLowerCase();
} catch (Exception e) {
zipNameAlias = zipName;
}
String encodeName;
if (zipNameAlias.endsWith(".zip")) {
encodeName = com.catxcloud.core.util.StringUtils.substringBeforeLast(zipName, ".");
} else {
encodeName = zipName;
}
ZipArchiveOutputStream zous;
try {
if (outputStream == null) {
outputStream = response.getOutputStream();
}
zous = new ZipArchiveOutputStream(outputStream);
zous.setUseZip64(Zip64Mode.AsNeeded);
String newZipName = encodeChineseDownloadFileName(request, encodeName) + ".zip";
response.setHeader("Content-disposition", "attachment; filename=" + newZipName);
} catch (Exception e) {
throw new GenericException(BusCodeEnum.BATCH_DOWN_LOAD_FILE_FAIL.getCode(),
BusCodeEnum.BATCH_DOWN_LOAD_FILE_FAIL.getMsg());
}
for (ByteFileBean bean : fileBeans) {
if (bean == null) {
continue;
}
if (com.catxcloud.core.util.StringUtils.isBlank(bean.getFileName())) {
Log.error("[批量下载文件并打包成zip服务]当前文件文件名为空,无法下载");
continue;
}
if (bean.getFileContent() == null || bean.getFileContent().length == 0) {
Log.error("[批量下载文件并打包成zip服务]文件名为:" + bean.getFileName() + "的文件,文件内容为空无法下载");
continue;
}
try {
ArchiveEntry entry = new ZipArchiveEntry(bean.getFileName());
zous.putArchiveEntry(entry);
zous.write(bean.getFileContent(), 0, bean.getFileContent().length);
zous.closeArchiveEntry();
} catch (Exception e) {
Log.error("[批量下载文件并打包成zip服务]文件名为:" + bean.getFileName() + "的文件打进zip出错", e);
}
}
try {
if (zous != null) {
zous.flush();
zous.close();
}
} catch (Exception e) {
Log.error("[批量下载文件并打包成zip服务]出错", e);
throw new GenericException(BusCodeEnum.BATCH_DOWN_LOAD_FILE_FAIL.getCode(),
BusCodeEnum.BATCH_DOWN_LOAD_FILE_FAIL.getMsg());
}
}
}