@PostMapping("/download/taskTemplate")
@WoAddUser
@WoAuditLog(value = AuditLogTypeConstant.QUERY)
public void downloadTaskTemplate(HttpServletResponse response, @RequestBody @Validated LanguageBody languageBody) {
ThreadUtils.renameThread("downloadTaskTemplate");
iDownloadService.downTaskExcelTemplate(response, languageBody);
}
void downTaskExcelTemplate(HttpServletResponse response, LanguageBody languageBody);
@Override
public void downTaskExcelTemplate(HttpServletResponse response, LanguageBody languageBody) {
String filePath = null;
if (EN.equals(languageBody.getLanguage())) {
filePath = "/excelTemplate/" + "task_en.zip";
} else {
filePath = "/excelTemplate/" + "task_cn.zip";
}
FileUtil.downloadZip(response, filePath);
}
public static void downloadZip(HttpServletResponse response, String filePath) {
OutputStream outputStream = null;
InputStream input = null;
BufferedInputStream bis = null;
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-zip-compressed;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=template.zip");
try {
ClassPathResource cpr = new ClassPathResource(filePath);
input = cpr.getInputStream();
byte[] buff = new byte[1024];
bis = new BufferedInputStream(input);
outputStream = response.getOutputStream();
int i = bis.read(buff);
while (i != -1) {
outputStream.write(buff, 0, buff.length);
outputStream.flush();
i = bis.read(buff);
}
outputStream.close();
} catch (IOException e) {
LOG.error("download task excel IOException failed!");
} finally {
CommonUtils.close(outputStream);
CommonUtils.close(input);
CommonUtils.close(bis);
}
}
}
public static void close(Closeable stream) {
if (null != stream) {
try {
stream.close();
} catch (IOException e) {
LOGGER.error("close stream failed!");
}
}
}