改善了文档下载,当传过来错误地址的时候,如果为单个文件,会给用户下载一个ErrorFile文件,内容为XXX文件不存在,当多个文件的时候,压缩包里会有正确路径的文件,和一个错误文件,内容为哪写文件不存在。
@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;
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)){
ServletOutputStream 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);
}
if (bis != null) {
bis.close();
}
if (in != null) {
in.close();
}
if (os != null) {
os.close();
}
}else {
String errorFilePathName=MyFileUtils.getHeaderFileName(filename)+" Not Exist";
String errorFilePath = MyFileUtils.getErrorFilePath() + MyFileUtils.getHeaderFileName(errorFilePathName) ;
File errorFile = new File(errorFilePath);
if (!errorFile.exists()) {
errorFile.mkdirs();
}
String errorName="ErrorFile.txt";
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(errorName, "UTF-8"));
RandomAccessFile raf = new RandomAccessFile(errorFilePath+File.separator+errorName,"rw");
String errorMsg="ErrorMessage: "+filename+" 文件不存在!";
String str=new String(errorMsg.getBytes("gbk"), "iso8859-1");
raf.writeBytes(str);
in = new FileInputStream(errorFile+File.separator+errorName);
ServletOutputStream os = response.getOutputStream();
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);
}
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();
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+" 文件不存在!";
try {
errorFiles = new File(errorFilesPath+File.separator+"ErrorFile.txt");
fw = new FileWriter(errorFiles, true);
} catch (IOException e) {
e.printStackTrace();
}
PrintWriter pw = new PrintWriter(fw);
pw.println(errorMsg);
pw.flush();
try {
fw.flush();
pw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (FileUtil.isFile(errorFiles)){
in = new FileInputStream(errorFiles);
ZipEntry zipEntry = new ZipEntry("ErrorFile.txt");
zos.putNextEntry(zipEntry);
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);
}
fw = new FileWriter(errorFiles, false);
fw.close();
}
if (bis != null) {
bis.close();
}
if (in != null) {
in.close();
}
if (zos != null) {
zos.close();
}
}