java web ftp 打包下載檔案
阿新 • • 發佈:2019-01-11
java web ftp 打包下載檔案
import org.apache.commons.io.FileUtils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java. util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 多檔案下載
* @param fileSns 檔案sn
* @param fileName 檔名稱sn
* @param response 響應
* @return 檢視
*/
@RequestMapping(value = "/download-files", method = RequestMethod.GET)
public void downloadFiles(@RequestParam List<Long> fileSns, String fileName, HttpServletResponse response) {
if (fileSns == null || fileSns.isEmpty()) {
throw new RuntimeException("檔案為空");
}
//獲取ftp儲存的檔案資訊
List<File> files = fileService.findListBySn(fileSns);
if (files.isEmpty()) {
throw new RuntimeException("沒有找到檔案");
}
//臨時檔案
java.io.File tempFile;
//Zip工具
ZipOutputStream out = null;
//獲取臨時檔案目錄
java.io.File fileDirecotry = FileUtils.getTempDirectory();
if (StringUtils.isNotBlank(fileName)) {
fileName += "-" + System.currentTimeMillis();
} else {
fileName = UUID.randomUUID().toString();
}
try {
//建立臨時zip檔案
tempFile = java.io.File.createTempFile(fileName, ".zip", fileDirecotry);
out = new ZipOutputStream(new FileOutputStream(tempFile));
List<String> names = new ArrayList<>();
//將多個檔案寫入zip
for (File file : files) {
//檔名去重
String name = distinctName(names, file.getOriginal());
//往zip檔案流中寫檔案
out.putNextEntry(new ZipEntry(name));
//省去ftp連結
ftpClient.retrieveFile(file.getPath(), out)
out.flush();
out.closeEntry();
}
//所有檔案寫完後一定要關閉輸出流,否則檔案下載會不完整。
out.flush();
out.close();
//將zip檔案寫入響應
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", "attachment; filename=" + new String(tempFile.getName().getBytes("UTF-8"), "ISO-8859-1"));
OutputStream os = new BufferedOutputStream(response.getOutputStream());
InputStream in = new BufferedInputStream(new FileInputStream(tempFile));
int len;
byte[] buf = new byte[1024];
while ((len = in.read(buf)) > 0) {
os.write(buf, 0, len);
}
os.flush();
os.close();
in.close();
response.flushBuffer();
FileUtils.deleteQuietly(tempFile);
} catch (Exception e) {
e.printStackTrace();
if (out != null) {
try {
out.close();
} catch (IOException ie) {
ie.printStackTrace();
}
}
throw new RuntimeException();
}
}
/**
* 檔名去重
* @param names 名稱集合
* @param name 檔名
* @return 去重檔名
*/
private String distinctName(List<String> names, String name) {
if (!names.contains(name)) {
names.add(name);
return name;
}
int count = 0;
int pos = name.lastIndexOf(StringUtils.DOT);
String namePrev = name;
String nameEnd = "";
if (pos > 0) {
namePrev = name.substring(0, pos);
nameEnd = name.substring(pos);
}
String tmpName;
do {
count++;
tmpName = namePrev + StringUtils.LEFT_BRACKET + count + StringUtils.RIGHT_BRACKET + nameEnd;
} while (names.contains(tmpName));
names.add(tmpName);
return tmpName;
}