gpt4 book ai didi

java - Netty似乎忽略了编码

转载 作者:行者123 更新时间:2023-11-30 04:32:17 26 4
gpt4 key购买 nike

我有一个正在运行的 netty 服务器,它似乎忽略了编码。 d3.js 中的错误是由于 PI 符号引起的。下面是设置编码的代码。即使在硬编码之后它仍然不起作用,有什么想法吗?

RandomAccessFile raf;
try {
raf = new RandomAccessFile(file, "r");
} catch (FileNotFoundException fnfe) {
sendError(ctx, NOT_FOUND);
return;
}
long fileLength = raf.length();

HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
setContentTypeHeader(response, file);
setContentLength(response, fileLength);
setDateAndCacheHeaders(response, file);
if (isKeepAlive(request)) {
response.setHeader(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
}

// Write the initial line and the header.
ctx.write(response);

// Write the content.
ChunkedFile chunkedFile = new ChunkedFile(raf, 0, fileLength, 8192);
ChannelFuture writeFuture = ctx.write(chunkedFile);

ctx.write(chunkedFile, writeFuture);

这是setContentTypeHeader代码:

private static void setContentTypeHeader(HttpResponse response, File file) {
String contentType = MimeTypes.getContentType(file.getPath());
response.setHeader(CONTENT_TYPE, contentType);
if (!contentType.equals("application/octet-stream")) {
response.setHeader(CONTENT_ENCODING, "charset=utf-8");
}

}

最佳答案

Content encoding不是字符编码,而是用于gzip等压缩。响应的字符编码在Content-Type header中指定。 .

private static void setContentTypeHeader(HttpResponse response, File file) {

String contentType = MimeTypes.getContentType(file.getPath());

if (!contentType.equals("application/octet-stream")) {
contentType += "; charset=utf-8";
}
response.setHeader(CONTENT_TYPE, contentType);

}

关于java - Netty似乎忽略了编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14356906/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com