FileDownload.java文件恢复

master
Guo XIn 1 year ago
parent b180cbda70
commit 8f94adfdb5
  1. 89
      shandan-common/src/main/java/com/keyware/shandan/common/util/FileDownload.java

@ -1,5 +1,7 @@
package com.keyware.shandan.common.util;
import org.springframework.util.Assert;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
@ -8,43 +10,80 @@ import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
/**
* 文件下載工具类
*/
public class FileDownload {
public static String download(HttpServletResponse response, File file, boolean deleteFile) {
return download(response, file, file.getName(), deleteFile);
/**
* @param response HttpServletResponse
* @param file File
* @return 结果
*/
public static String download(HttpServletResponse response, File file) {
return download(response, file, null, false);
}
/**
* @param response HttpServletResponse
* @param file File
* @param fileName 输出的文件名
* @return 结果
*/
public static String download(HttpServletResponse response, File file, String fileName) {
return download(response, file, fileName, false);
}
public static String download(HttpServletResponse response, File file, String fileName, boolean deleteFile) {
try (BufferedInputStream bis = new BufferedInputStream(Files.newInputStream(file.toPath()));
OutputStream out = response.getOutputStream()) {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream");
byte[] fileNameBytes = fileName.getBytes(StandardCharsets.UTF_8);
response.setHeader("Content-disposition", "attachment;filename=" + new String(fileNameBytes, 0, fileNameBytes.length, StandardCharsets.ISO_8859_1));
byte[] buffer = new byte[1024]; // 缓冲区
int bytesToRead = 0;
// 通过循环将读入的Word文件的内容输出到浏览器中
while ((bytesToRead = bis.read(buffer)) != -1) {
out.write(buffer, 0, bytesToRead);
out.flush();
/**
* @param response HttpServletResponse
* @param file File
* @param delete 下载完成后是否删除源文件
* @return 结果
*/
public static String download(HttpServletResponse response, File file, boolean delete) {
return download(response, file, null, delete);
}
/**
* @param response HttpServletResponse
* @param file File
* @param fileName 输出的文件名称
* @param delete 下载完成后是否删除源文件
* @return 结果
*/
public static String download(HttpServletResponse response, File file, String fileName, boolean delete) {
Assert.notNull(response, "response is null.");
Assert.notNull(file, "file is null.");
Assert.isTrue(file.exists(), "没有在文件系统中找到该文件.");
response.reset();
response.setContentType("application/octet-stream");
response.setCharacterEncoding("utf-8");
response.setContentLength((int) file.length());
if (!StringUtils.hasText(fileName)) {
fileName = file.getName();
}
// 解决下载文件时文件名乱码问题
byte[] fileNameBytes = fileName.getBytes(StandardCharsets.UTF_8);
String outputName = new String(fileNameBytes, 0, fileNameBytes.length, StandardCharsets.ISO_8859_1);
response.setHeader("Content-Disposition", "attachment;filename=" + outputName);
try (OutputStream os = response.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(Files.newInputStream(file.toPath()))) {
byte[] buff = new byte[1024];
int i = 0;
while ((i = bis.read(buff)) != -1) {
os.write(buff, 0, i);
os.flush();
}
} catch (IOException e) {
throw new RuntimeException(e);
return "下载过程出现错误,文件流读取异常!";
} finally {
if (deleteFile) {
try {
Thread.sleep(100);
} catch (InterruptedException ignored) {
}
if (delete) {
file.deleteOnExit();
}
}
return "download success";
return "下载完成";
}
}