如何从 JAX-RS 响应中解析字符集值?
How can I parse charset value from the JAX-RS response?
在 Content-Type
可能有 charset
属性 的任何 Response
上,
我如何解析该值?
HTTP/1.1 200 OK
Content-Type: plain/text; charset=<UNKNOWN>
当客户端需要解析为 Reader
时,他们如何检测 charset
值?
Response response = request();
try (InputStream stream = response.readEntity(InputStream.class);
InputStreamReader reader = new InputStreamReader(stream, ????);
BufferedReader buffered = new BufferedReader(reader)) {
}
任何人都可以使用 MediaType#getParameters。
final MediaType mediaType = response.getMediaType();
// mediaType may be null
final Map<String, String> parameters = mediaType.getParameters();
final String charset = parameters.get("charset");
// charset, again, may be null
在 Content-Type
可能有 charset
属性 的任何 Response
上,
我如何解析该值?
HTTP/1.1 200 OK
Content-Type: plain/text; charset=<UNKNOWN>
当客户端需要解析为 Reader
时,他们如何检测 charset
值?
Response response = request();
try (InputStream stream = response.readEntity(InputStream.class);
InputStreamReader reader = new InputStreamReader(stream, ????);
BufferedReader buffered = new BufferedReader(reader)) {
}
任何人都可以使用 MediaType#getParameters。
final MediaType mediaType = response.getMediaType();
// mediaType may be null
final Map<String, String> parameters = mediaType.getParameters();
final String charset = parameters.get("charset");
// charset, again, may be null