jdk1.8
package com.yitong.sny.common.utils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
/**
* 未完成,待补充,需要继续参考:
* https://www.jb51.net/article/241277.htm
* @date 2023-02-20
*/
public class DownloadUtil {
/**
* 设置下载
*
* @param response 此次响应的对象
* @param contentType 内容类型
* @param filename 文件名
* @throws UnsupportedEncodingException 转换文件名编码时出现错误
*/
public static void setDownloadInfo(HttpServletResponse response, String contentType, String filename) throws UnsupportedEncodingException {
response.setContentType(contentType);
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(filename, "UTF-8"));
}
/**
* 输出流
*
* @param response 响应
* @param contentType 资源类型
* @param filepath 文件路径
* @return 返回是否输出成功
* @throws UnsupportedEncodingException 不支持的编码异常
*/
public boolean output(HttpServletResponse response, String contentType, String filepath) throws UnsupportedEncodingException {
File file = new File(filepath);//创建下载的目标文件,参数为文件的真实路径
if (file.exists()) { //判断文件是否存在
setDownloadInfo(response, contentType, file.getName());
byte[] buffer = new byte[1024];
try (
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream() // 输出流
) {
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer);
i = bis.read(buffer);
}
return true;
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
/**
* 响应 resource 目录下的文件资源
*
* @param filepath resource 下的文件资源路径(相对路径,比如传入参数值:<code>"static/image/pic.png"</code>)
*/
public static ResponseEntity<Resource> getResponseResourceByClassPath(String filepath) throws IOException {
ClassPathResource resource = new ClassPathResource(filepath);
String returnName = new File(filepath).getName();
return getResponseResource(resource, returnName, MediaType.APPLICATION_OCTET_STREAM);
}
/**
* 响应到前端页面
*
* @return 返回响应的资源
*/
public static ResponseEntity<Resource> getResponseResource(String filepath) throws IOException {
Path path = new File(filepath).toPath();
MediaType mediaType;
if (new File(filepath).exists()) {
try {
String contentType = Files.probeContentType(path);
mediaType = MediaType.parseMediaType(contentType);
} catch (Exception e) {
mediaType = MediaType.APPLICATION_OCTET_STREAM;
}
} else {
mediaType = MediaType.APPLICATION_OCTET_STREAM;
}
return getResponseResource(filepath, mediaType);
}
public static ResponseEntity<Resource> getResponseResource(String filepath, MediaType mediaType) throws IOException {
Path path = new File(filepath).toPath();
Path fileName = path.getFileName();
return getResponseResource(filepath, fileName.toString(), mediaType);
}
public static ResponseEntity<Resource> getResponseResource(String filepath, String returnName, MediaType mediaType) throws IOException {
Path path = new File(filepath).toPath();
FileSystemResource resource = new FileSystemResource(path);
return getResponseResource(resource, returnName, mediaType);
}
public static ResponseEntity<Resource> getResponseResource(Resource resource, String returnName, MediaType mediaType) throws IOException {
returnName = URLEncoder.encode(returnName, "utf-8");
return ResponseEntity.ok()
.contentType(mediaType)
.header("Content-disposition", "attachment; filename=" + returnName)
.body(resource);
}
}