【发布时间】:2016-03-15 10:27:05
【问题描述】:
发送文件名 (Žluťoučký kůň.txt) 中以捷克语字符 UTF-8 编码的文件,由 RESTEasy 使用。但是在java中我总是变成US-ASCII文件名(当然是错误的)
用于发送文件的 HTML:
Select a file to upload: <br />
<form action="http://localhost/file/upload" method="post" enctype="multipart/form-data" accept-charset="UTF-8">
<input type="file" name="file" size="50" />
<input type="submit" value="Upload File" />
</form>
with 真的是发送:
------WebKitFormBoundaryAyBqNu6jIFHAB660
Content-Disposition: form-data; name="file"; filename="Žluťoučký kůň.txt"
Content-Type: text/plain
用于获取 UTF-8 编码的过滤器:
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
servletRequest.setCharacterEncoding("UTF-8");
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
requestContext.setProperty(InputPart.DEFAULT_CONTENT_TYPE_PROPERTY, "*/*; charset=UTF-8");
requestContext.setProperty(InputPart.DEFAULT_CHARSET_PROPERTY, "UTF-8");
读取多部分的Java代码:
public List<CaseFile> uploadFile(MultipartFormDataInput input, long caseId) {
MultipartFormDataOutput mdo = new MultipartFormDataOutput();
Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
for (List<InputPart> inputParts : uploadForm.values()) {
for (InputPart inputPart : inputParts) {
try {
// Retrieve headers, read the Content-Disposition header to obtain the original name of the file
MultivaluedMap<String, String> headers = inputPart.getHeaders(); //here are all headers in US-ASCII
和标题包含:
表格数据;名称=“文件”;文件名="??lu??ou??k??k??.txt"
【问题讨论】:
-
请澄清“//这里所有的标题都是 US-ASCII”。
-
您如何查看标题?您的输出流可能不支持这些字符。请使用 stackoverflow.com/questions/923863/… 以 UTF-8 对您的回复进行 hexify
-
不,我已经尝试过这样的转换——它真的只是 US-ASCII
-
我想高兴地哭泣。带有反射的黑客对我有帮助。
Field f = inputPart.getClass().getDeclaredField("bodyPart"); f.setAccessible(true); BodyPart bodyPart = (BodyPart) f.get(inputPart); Header header = bodyPart.getHeader(); ...
标签: encoding utf-8 character-encoding resteasy multipartform-data