Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#578 fix(filename): 有些 url 不包含文件名,且是自动生成的,不能自定义,因此在获取文件名称的时候会出错, 增加额外… #579

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
21 changes: 21 additions & 0 deletions server/src/main/java/cn/keking/service/FileHandlerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -496,6 +498,25 @@ public FileAttribute getFileAttribute(String url, HttpServletRequest req) {
cacheFilePrefixName = originFileName.substring(0, originFileName.lastIndexOf(".")) + suffix + "."; //这里统一文件名处理 下面更具类型 各自添加后缀
} catch (Exception e) {
logger.error("获取文件名后缀错误:", e);
try {
logger.info("尝试通过 Content-Disposition 获取后缀:", e);
URL obj = new URL(url);
URLConnection conn = obj.openConnection();
String headerField = conn.getHeaderField("Content-Disposition");
for (String s : headerField.split(";")) {
int idx = s.indexOf("filename");
if (idx > -1) {
s = s.replaceAll("[\"\\s]*", "");
originFileName = s.substring(idx + 9);
cacheFilePrefixName = originFileName.substring(0, originFileName.lastIndexOf(".")) + suffix + ".";
isHtmlView = suffix.equalsIgnoreCase("xls") || suffix.equalsIgnoreCase("xlsx") || suffix.equalsIgnoreCase("csv") || suffix.equalsIgnoreCase("xlsm") || suffix.equalsIgnoreCase("xlt") || suffix.equalsIgnoreCase("xltm") || suffix.equalsIgnoreCase("et") || suffix.equalsIgnoreCase("ett") || suffix.equalsIgnoreCase("xlam");
type = FileType.typeFromFileName(originFileName);
suffix = KkFileUtils.suffixFromFileName(originFileName);
break;
}
}
}catch (Exception ignore){}

// e.printStackTrace();
}
String cacheFileName = this.getCacheFileName(type, originFileName, cacheFilePrefixName, isHtmlView, isCompressFile);
Expand Down