【发布时间】:2020-04-01 21:24:55
【问题描述】:
给定一个 HTTP 标头,例如:
Content-Type: text/plain; charset=something
我想使用完全符合 RFC 的解析来提取 MIME 类型和字符集,但不“验证”字符集。通过验证,我的意思是我不想使用 Java 的内部字符集机制,以防 Java 不知道字符集(但可能对其他应用程序仍然有意义)。以下代码不起作用,因为它执行此验证:
import org.apache.http.entity.ContentType;
String header = "text/plain; charset=something";
ContentType contentType = ContentType.parse(header);
Charset contentTypeCharset = contentType.getCharset();
System.out.println(contentType.getMimeType());
System.out.println(contentTypeCharset == null ? null : contentTypeCharset.toString());
这会抛出java.nio.charset.UnsupportedCharsetException: something。
【问题讨论】:
标签: java mime-types