Skip to content

Commit 748c19f

Browse files
committed
🐛 binarywang#3348 【公共问题】修复无法获取正确文件名的问题
1 parent b6a8721 commit 748c19f

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/HttpResponseProxy.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
import org.apache.http.Header;
77
import org.apache.http.client.methods.CloseableHttpResponse;
88

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+
915
/**
1016
* <pre>
1117
* 三种http框架的response代理类,方便提取公共方法
@@ -56,29 +62,42 @@ private String getFileName(CloseableHttpResponse response) throws WxErrorExcepti
5662
throw new WxErrorException("无法获取到文件名,Content-disposition为空");
5763
}
5864

59-
return this.extractFileNameFromContentString(contentDispositionHeader[0].getValue());
65+
return extractFileNameFromContentString(contentDispositionHeader[0].getValue());
6066
}
6167

6268
private String getFileName(HttpResponse response) throws WxErrorException {
6369
String content = response.header("Content-disposition");
64-
return this.extractFileNameFromContentString(content);
70+
return extractFileNameFromContentString(content);
6571
}
6672

6773
private String getFileName(Response response) throws WxErrorException {
6874
String content = response.header("Content-disposition");
69-
return this.extractFileNameFromContentString(content);
75+
return extractFileNameFromContentString(content);
7076
}
7177

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()) {
7480
throw new WxErrorException("无法获取到文件名,content为空");
7581
}
7682

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);
82101
}
83102

84103
throw new WxErrorException("无法获取到文件名,header信息有问题");
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package me.chanjar.weixin.common.util.http;
2+
3+
import me.chanjar.weixin.common.error.WxErrorException;
4+
import org.testng.annotations.Test;
5+
6+
import static org.testng.Assert.*;
7+
8+
public class HttpResponseProxyTest {
9+
10+
@Test
11+
public void testExtractFileNameFromContentString() throws WxErrorException {
12+
String content = "attachment; filename*=utf-8''%E6%B5%8B%E8%AF%95.xlsx; filename=\"��.xlsx\"";
13+
String filename = HttpResponseProxy.extractFileNameFromContentString(content);
14+
assertNotNull(filename);
15+
assertEquals(filename, "测试.xlsx");
16+
}
17+
18+
@Test
19+
public void testExtractFileNameFromContentString_another() throws WxErrorException {
20+
String content = "attachment; filename*=utf-8''%E8%90%A5%E4%B8%9A%E6%89%A7%E7%85%A7.jpg; filename=\"����.jpg\"";
21+
// String content = "attachment; filename=\"����.jpg\"";
22+
String filename = HttpResponseProxy.extractFileNameFromContentString(content);
23+
assertNotNull(filename);
24+
assertEquals(filename, "营业执照.jpg");
25+
}
26+
}

0 commit comments

Comments
 (0)