Java 内蒂似乎忽略了编码
我有一个netty服务器正在运行,它似乎忽略了编码。d3.js中的错误是由PI符号引起的。下面是设置编码的代码。即使在硬编码之后,它仍然不起作用,你知道为什么吗Java 内蒂似乎忽略了编码,java,encoding,utf-8,netty,Java,Encoding,Utf 8,Netty,我有一个netty服务器正在运行,它似乎忽略了编码。d3.js中的错误是由PI符号引起的。下面是设置编码的代码。即使在硬编码之后,它仍然不起作用,你知道为什么吗 RandomAccessFile raf; try { raf = new RandomAccessFile(file, "r"); } catch (FileNotFoundException fnfe) { sendError(ctx, NOT_FOUND); return; } long fileLength = ra
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");
}
}不是字符编码,而是用于压缩,如gzip。响应的字符编码在中指定
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);
}