|
6 | 6 | import org.apache.http.Header;
|
7 | 7 | import org.apache.http.client.methods.CloseableHttpResponse;
|
8 | 8 |
|
| 9 | +import java.io.UnsupportedEncodingException; |
| 10 | +import java.net.URLDecoder; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.util.regex.Matcher; |
| 13 | +import java.util.regex.Pattern; |
| 14 | + |
9 | 15 | /**
|
10 | 16 | * <pre>
|
11 | 17 | * 三种http框架的response代理类,方便提取公共方法
|
@@ -56,29 +62,42 @@ private String getFileName(CloseableHttpResponse response) throws WxErrorExcepti
|
56 | 62 | throw new WxErrorException("无法获取到文件名,Content-disposition为空");
|
57 | 63 | }
|
58 | 64 |
|
59 |
| - return this.extractFileNameFromContentString(contentDispositionHeader[0].getValue()); |
| 65 | + return extractFileNameFromContentString(contentDispositionHeader[0].getValue()); |
60 | 66 | }
|
61 | 67 |
|
62 | 68 | private String getFileName(HttpResponse response) throws WxErrorException {
|
63 | 69 | String content = response.header("Content-disposition");
|
64 |
| - return this.extractFileNameFromContentString(content); |
| 70 | + return extractFileNameFromContentString(content); |
65 | 71 | }
|
66 | 72 |
|
67 | 73 | private String getFileName(Response response) throws WxErrorException {
|
68 | 74 | String content = response.header("Content-disposition");
|
69 |
| - return this.extractFileNameFromContentString(content); |
| 75 | + return extractFileNameFromContentString(content); |
70 | 76 | }
|
71 | 77 |
|
72 |
| - private String extractFileNameFromContentString(String content) throws WxErrorException { |
73 |
| - if (content == null || content.length() == 0) { |
| 78 | + public static String extractFileNameFromContentString(String content) throws WxErrorException { |
| 79 | + if (content == null || content.isEmpty()) { |
74 | 80 | throw new WxErrorException("无法获取到文件名,content为空");
|
75 | 81 | }
|
76 | 82 |
|
77 |
| - int startIndex = content.indexOf("filename=\""); |
78 |
| - if (startIndex != -1) { |
79 |
| - startIndex += "filename=\"".length(); |
80 |
| - int endIndex = content.indexOf('"', startIndex); |
81 |
| - return content.substring(startIndex, endIndex); |
| 83 | + // 查找filename*=utf-8''开头的部分 |
| 84 | + Pattern pattern = Pattern.compile("filename\\*=utf-8''(.*?)($|;|\\s|,)"); |
| 85 | + Matcher matcher = pattern.matcher(content); |
| 86 | + if (matcher.find()) { |
| 87 | + String encodedFileName = matcher.group(1); |
| 88 | + // 解码URL编码的文件名 |
| 89 | + try { |
| 90 | + return URLDecoder.decode(encodedFileName, StandardCharsets.UTF_8.name()); |
| 91 | + } catch (UnsupportedEncodingException e) { |
| 92 | + throw new RuntimeException(e); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // 查找普通filename="..."部分 |
| 97 | + pattern = Pattern.compile("filename=\"(.*?)\""); |
| 98 | + matcher = pattern.matcher(content); |
| 99 | + if (matcher.find()) { |
| 100 | + return new String(matcher.group(1).getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); |
82 | 101 | }
|
83 | 102 |
|
84 | 103 | throw new WxErrorException("无法获取到文件名,header信息有问题");
|
|
0 commit comments