通过将错误信息写成字符,然后直接写入字符流。
@Override
public void downloadTranslationFiles(List<String> filePath, HttpServletResponse response) throws IOException {
if (filePath.size() <= 1) {
for (String fp : filePath) {
downloadFilesForOne( fp, response);
}
} else {
downloadFilesForZIP(filePath, response);
}
}
public void downloadFilesForOne(String filePath, HttpServletResponse response) throws IOException {
FileSystemResource fileSystemResource = null;
FileInputStream in = null;
String filename = null;
BufferedInputStream bis = null;
ServletOutputStream os=null;
filename = filePath.substring(filePath.lastIndexOf("\\") + 1);
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
fileSystemResource = new FileSystemResource(filePath);
File file = fileSystemResource.getFile();
if (FileUtil.isFile(file)){
os = response.getOutputStream();
in = new FileInputStream(file);
bis = new BufferedInputStream(in, 1024 * 10);
byte[] buf = new byte[1024 * 10];
int len = 0;
while ((len = bis.read(buf, 0, 1024 * 10)) > 0) {
os.write(buf, 0, len);
}
}else {
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("ErrorFile_"+System.currentTimeMillis()+".txt", "UTF-8"));
String errorMsg="ErrorMessage: "+filename+" 文件不存在!";
byte[] buf = errorMsg.getBytes(StandardCharsets.UTF_8);
os = response.getOutputStream();
os.write(buf);
}
if (bis != null) {
bis.close();
}
if (in != null) {
in.close();
}
if (os != null) {
os.close();
}
}
public void downloadFilesForZIP(List<String> filePath, HttpServletResponse response) throws IOException {
OutputStream os = null;
FileSystemResource fileSystemResource = null;
FileInputStream in = null;
ZipOutputStream zos = null;
String FilePath = null;
String filename = null;
BufferedInputStream bis = null;
File errorFiles = null;
String errorFilesPath = null;
FileWriter fw = null;
os = response.getOutputStream();
List<String> list = new ArrayList<>();
zos = new ZipOutputStream(os);
String zipFileName = System.currentTimeMillis() + "_dataFiles.zip";
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipFileName, "UTF-8"));
for (String fp : filePath) {
FilePath = fp;
filename = FilePath.substring(FilePath.lastIndexOf("\\") + 1);
fileSystemResource = new FileSystemResource(FilePath);
File file = fileSystemResource.getFile();
if (FileUtil.isFile(file)) {
ZipEntry zipEntry = new ZipEntry(filename);
zos.putNextEntry(zipEntry);
in = new FileInputStream(file);
bis = new BufferedInputStream(in, 1024 * 10);
byte[] buf = new byte[1024 * 10];
int len = 0;
while ((len = bis.read(buf, 0, 1024 * 10)) > 0) {
zos.write(buf, 0, len);
}
} else {
errorFilesPath = MyFileUtils.getErrorFilePath() + "ErrorFile";
File errorFilesFolder = new File(errorFilesPath);
if (!errorFilesFolder.exists()) {
errorFilesFolder.mkdirs();
}
String errorMsg = "ErrorMessage: " + filename + " 文件不存在!"+"\n";
list.add(errorMsg);
}
}
ZipEntry zipEntry = new ZipEntry("ErrorFile_"+System.currentTimeMillis()+".txt");
zos.putNextEntry(zipEntry);
for (String str:list){
byte[] buf = str.getBytes(StandardCharsets.UTF_8);
zos.write(buf);
}
if (bis != null) {
bis.close();
}
if (in != null) {
in.close();
}
if (zos != null) {
zos.close();
}
}