- 文件下载方法
@RequestMapping(value = "/download", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<InputStreamResource> download(){
ResponseEntity<InputStreamResource> responseEntity = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
String zipPath = filePath + File.separator + UUID.randomUUID() + File.separator + LocalDate.now();
String zipPathName = zipPath + Constants.REPORT_FILENAME_EXT_ZIP;
File zipFolder = new File(zipPath);
if (!zipFolder.exists()) {
zipFolder.mkdirs();
}
File zipFile = new File(zipPathName);
if (!zipFile.exists()) {
zipFile.createNewFile();
}
compreSsion(zipPathName, new File(zipPath));
FileSystemResource file = new FileSystemResource(zipPathName);
if (file.exists()) {
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getFilename()));
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
return responseEntity
.ok()
.headers(headers)
.contentLength(file.contentLength())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(new InputStreamResource(file.getInputStream()));
}
return null;
}
- 文件压缩方法
private static void compreSsion(String zipFileName, File target) throws IOException {
logger.info("下载->压缩文件...");
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
BufferedOutputStream bos = new BufferedOutputStream(out);
zip(out, target, target.getName());
bos.close();
out.close();
logger.info("下载->压缩完成");
}
public static void compreSsion(String zipFileName, List<String> target, String path) throws IOException {
logger.info("月结账单下载->压缩文件...");
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
BufferedOutputStream bos = new BufferedOutputStream(out);
for (String s : target) {
File folder = new File(path + File.separator + s);
if (Objects.nonNull(folder.list())) {
zip(out, folder, folder.getName());
}
}
bos.close();
out.close();
logger.info("月结账单下载->压缩完成");
}
private static void zip(ZipOutputStream zout, File target, String name) throws IOException {
if (target.isDirectory()) {
File[] files = target.listFiles();
if (files.length == 0) {
zout.putNextEntry(new ZipEntry(name + "/"));
}
for (File f : files) {
zip(zout, f, name + "/" + f.getName());
}
} else {
zout.setEncoding("GBK");
zout.putNextEntry(new ZipEntry(name));
InputStream inputStream = new FileInputStream(target);
BufferedInputStream bis = new BufferedInputStream(inputStream);
byte[] b = new byte[8192];
int length = 0;
while ((length = bis.read(b)) != -1) {
zout.write(b, 0, length);
}
bis.close();
}
}
- vue下载文件方法
window.open(window._global._BASEURL + '***/***?param=' + this.xxx);
onDownload(param) {
this.$httpExt().get('/report/excel?param=param', {
xxx: this.searchForm.xxx,
xxx: this.searchForm.xxx,
xxx: this.searchForm.xxx,
}, {
responseType: 'arraybuffer'
}).then(res => {
if (res && res.status === 200) {
this.$message({
message: this.$t('文件正在下载,请稍后!'),
type: 'success',
duration: 2000
});
const [, fileName] = res.headers['content-disposition'].split('=');
const blob = new Blob([res.data]);
if ('download' in document.createElement('a')) {
const downloadElement = document.createElement('a');
const href = window.URL.createObjectURL(blob);
downloadElement.href = href;
downloadElement.download = decodeURI(fileName);
downloadElement.style.display = 'none';
document.body.appendChild(downloadElement);
downloadElement.click();
document.body.removeChild(downloadElement);
window.URL.revokeObjectURL(href);
} else {
navigator.msSaveblob(blob, fileName)
}
});
}
}