package com.synda.utils;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
public class DownloadUtil {
private static final Logger log = LoggerFactory.getLogger(DownloadUtil.class);
public static void downloadFileByPath(HttpServletResponse response,String filePath,boolean isOnLine){
setResponse(response,null,filePath,"","",isOnLine);
download(response,new File(filePath));
}
public static void downloadFileByPath(HttpServletResponse response,String filePath,String contentType,boolean isOnLine){
setResponse(response,null,filePath,contentType,"",isOnLine);
download(response,new File(filePath));
}
public static void downloadFileByPath(HttpServletResponse response,String filePath,String contentType,String fileName,boolean isOnLine){
setResponse(response,null,filePath,contentType,fileName,isOnLine);
download(response,new File(filePath));
}
public static void downloadFile(HttpServletResponse response,File file){
setResponse(response,file,"","","",false);
download(response,file);
}
public static void downloadFile(HttpServletResponse response,File file,String contentType,boolean isOnLine){
setResponse(response,file,"",contentType,"",isOnLine);
download(response,file);
}
public static void downloadFile(HttpServletResponse response,File file,String contentType,String fileName,boolean isOnLine){
setResponse(response,file,"",contentType,fileName,isOnLine);
download(response,file);
}
public static void downloadFileByStream(HttpServletResponse response,FileInputStream inputStream,String contentType,String fileName,boolean isOnLine){
setResponse(response,contentType,fileName,isOnLine);
download(response,inputStream);
}
public static void downloadFileByByte(HttpServletResponse response,byte[] data,String contentType,String fileName,boolean isOnLine){
setResponse(response,contentType,fileName,isOnLine);
download(response,data);
}
private static void setResponse(HttpServletResponse response,File file,String filePath,String contentType,String fileName,boolean isOnLine){
if (file==null) file = new File(filePath);
fileName = StringUtils.hasText(fileName) ? fileName: file.getName();
setResponse(response,contentType,fileName,isOnLine);
}
private static void setResponse(HttpServletResponse response,String contentType,String fileName,boolean isOnLine){
contentType = StringUtils.hasText(contentType) ? contentType: "application/octet-stream";
response.setCharacterEncoding("UTF-8");
response.setContentType(contentType);
if (isOnLine){
response.setHeader("Content-Disposition", "inline; filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));
}else {
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));
}
}
private static void download(HttpServletResponse response,File file){
try {
FileInputStream fileInputStream = new FileInputStream(file);
download(response,fileInputStream);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
private static void download(HttpServletResponse response,byte[] data){
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
download(response,byteArrayInputStream);
}
private static void download(HttpServletResponse response,InputStream inputStream){
byte[] buffer = new byte[1024];
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(inputStream);
response.setHeader("Content-Length", String.valueOf(inputStream.available()));
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
log.info("File download successfully!");
os.close();
} catch (Exception e) {
e.printStackTrace();
log.info("File download failed !");
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}