1
我有一個netty服務器運行,似乎忽略了編碼。由於PI符號導致d3.js中的錯誤。以下是設置編碼的代碼。即使在硬編碼之後它仍然不起作用,爲什麼?Netty似乎無視編碼
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");
}
}