Skip to content

Commit

Permalink
修复扩展ajax模拟响应乱码问题
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie committed Nov 2, 2018
1 parent 08a2ce9 commit 000309d
Showing 1 changed file with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.GZIPInputStream;
import org.springframework.util.StringUtils;

public class XMLHttpRequest {

Expand Down Expand Up @@ -72,12 +75,21 @@ public void send(String data) throws IOException {
}
);
readystatechange(2, code);
String charset = "UTF-8";
String contentType = connection.getContentType();
if (!StringUtils.isEmpty(contentType)) {
Pattern pattern = Pattern.compile("charset=(.*)$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(contentType);
if (matcher.find()) {
charset = matcher.group(1);
}
}
InputStream inputStream = code != 200 ? connection.getErrorStream() : connection.getInputStream();
if (responseHeads.entrySet().stream().anyMatch(entry -> "Content-Encoding".equalsIgnoreCase(entry.getKey()) && entry.getValue().matches("^.*(?i)(gzip).*$"))) {
inputStream = new GZIPInputStream(inputStream);
}
try (
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset))
) {
readystatechange(3, code);
responseText = reader.lines().collect(Collectors.joining("\n"));
Expand Down

0 comments on commit 000309d

Please sign in to comment.