后端代码
public void viewAndDownload(String filePath, String uploadpath, String uploadType, HttpServletResponse response) {
InputStream inputStream = null;
ServletOutputStream outputStream = null;
try {
String fileName;
String bucketName;
if (filePath.startsWith("http")) {
fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
String objectName;
if ("minio".equals(uploadType)) {
bucketName = filePath.replace(MinioUtil.getMinioUrl(), "").split("/")[0];
objectName = filePath.replace(MinioUtil.getMinioUrl() + bucketName, "");
inputStream = MinioUtil.getMinioFile(bucketName, objectName);
if (inputStream == null) {
bucketName = "eoafile";
objectName = filePath.replace(OssBootUtil.getStaticDomain() + "/", "");
inputStream = OssBootUtil.getOssFile(objectName, bucketName);
}
} else {
bucketName = "eoafile";
objectName = filePath.replace(OssBootUtil.getStaticDomain() + "/", "");
inputStream = OssBootUtil.getOssFile(objectName, bucketName);
if (inputStream == null) {
bucketName = filePath.replace(MinioUtil.getMinioUrl(), "").split("/")[0];
objectName = filePath.replace(MinioUtil.getMinioUrl() + bucketName, "");
inputStream = MinioUtil.getMinioFile(bucketName, objectName);
}
}
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("UTF-8"), "iso-8859-1"));
} else {
filePath = filePath.replace("..", "");
if (filePath.endsWith(",")) {
filePath = filePath.substring(0, filePath.length() - 1);
}
fileName = uploadpath + File.separator + filePath;
bucketName = uploadpath + File.separator + fileName;
File file = new File(bucketName);
inputStream = new BufferedInputStream(new FileInputStream(fileName));
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes("UTF-8"), "iso-8859-1"));
}
response.setContentType("application/force-download");
outputStream = response.getOutputStream();
if (inputStream != null) {
byte[] buf = new byte[1024];
int len;
while((len = ((InputStream)inputStream).read(buf)) > 0) {
outputStream.write(buf, 0, len);
}
response.flushBuffer();
}
} catch (IOException var22) {
response.setStatus(404);
this.log.error("预览文件失败" + var22.getMessage());
} finally {
if (inputStream != null) {
try {
((InputStream)inputStream).close();
} catch (IOException var21) {
this.log.error(var21.getMessage(), var21);
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException var20) {
this.log.error(var20.getMessage(), var20);
}
}
}
}
前端代码
export function downFile(url, parameter) {
return axios({
url: url,
params: parameter,
method: 'get',
responseType: 'blob'
})
}
exportData(){
var params = this.getQueryParams();
delete params.entrustStartTimeSearch
delete params.testStartTimeSearch
this.loading = true;
downFile(this.url.exportData,params).then((data)=>{
if (!data) {
this.$message.warning("导出失败")
return
}
let url = window.URL.createObjectURL(new Blob([data]));
let link = document.createElement('a');
link.style.display = 'none';
link.href = url;
link.setAttribute('download', '试验设备时长统计表.xlsx');
document.body.appendChild(link);
link.click()
this.$message.success("导出成功!")
this.loading = false;
})
}