1

エンコーディングを無視しているように見える netty サーバーを実行しています。PI シンボルが原因で d3.js のエラーが発生します。以下は、エンコーディングを設定するコードです。ハードコーディングした後でもまだ機能しませんが、その理由は何ですか?

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");
}

}

4

1 に答える 1

2

コンテンツのエンコーディングは文字エンコーディングではなく、gzip などの圧縮用です。応答の文字エンコーディングは、Content-Type ヘッダーで指定されます。

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);

}
于 2013-01-16T16:03:06.570 に答える