如何解析JAX-RS响应中的charset值?

时间:2016-08-06 13:01:40

标签: http character-encoding client jax-rs content-type

Response可能拥有Content-Type财产的任何charset上, 我该如何解析这个值?

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)) {
}

1 个答案:

答案 0 :(得分:0)

任何人都可以使用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