|
|
@@ -10,15 +10,13 @@ import com.mrxu.framework.common.util.BusinessException;
|
|
|
import com.mrxu.framework.common.util.StrFunc;
|
|
|
import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.util.ResourceUtils;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import javax.servlet.http.HttpSession;
|
|
|
-import java.io.File;
|
|
|
+import java.io.InputStream;
|
|
|
import java.io.OutputStream;
|
|
|
import java.net.URLEncoder;
|
|
|
-import java.nio.file.Files;
|
|
|
import java.util.Collection;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
@@ -269,17 +267,16 @@ public class BaseController {
|
|
|
*/
|
|
|
@SneakyThrows
|
|
|
protected void downloadFile(HttpServletResponse response,String fileName) {
|
|
|
- String path = this.getClass().getClassLoader().getResource("").getPath();
|
|
|
- log.info("resource 路径:{}",path);
|
|
|
- File file = new File(path + "file/"+fileName);
|
|
|
- // 文件转成字节数组
|
|
|
- byte[] fileByte = Files.readAllBytes(file.toPath());
|
|
|
+ InputStream fis = Thread.currentThread().getContextClassLoader().getResourceAsStream("file/"+fileName);
|
|
|
+ byte[] fileByte = new byte[fis.available()];
|
|
|
+ fis.read(fileByte);
|
|
|
+ fis.close();
|
|
|
// 设置response的Header
|
|
|
response.setCharacterEncoding("UTF-8");
|
|
|
// 指定下载文件名(attachment-以下载方式保存到本地,inline-在线预览)
|
|
|
response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\"");
|
|
|
// 告知浏览器文件的大小
|
|
|
- response.addHeader("Content-Length", "" + file.length());
|
|
|
+ response.addHeader("Content-Length", "" + fileByte.length);
|
|
|
// 内容类型为通用类型,表示二进制数据流
|
|
|
response.setContentType("application/octet-stream");
|
|
|
OutputStream os = response.getOutputStream();
|