I am currently using the jQuery-File-Upload. I may upload some files with a Japanese or Chinese file name, and I can see that the file name is for example, "お疲れ様です.txt" or "测试文档.txt" in browser's debug mode, but in the backend(Java), they become "ã�Šç–²ã‚Œæ§˜ã�§ã�™.txt" and "测试文档.txt".
I once tried to set formAcceptCharset to UTF-8 but it does not work.
Question:
How to get the correct file name in Java side when parsing the MultipartFormData?
Thanks in advance.
BTW, The following is my data
-----------------------------25382434931419
Content-Disposition: form-data; name="file"; filename="�疲れ様��.txt"
Content-Type: text/plain
....
Add the Java codes
In fact I did nothing in Java side currently,
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String upload(InMultiPart inMP) {
while (inMP.hasNext()) {
InPart part = inMP.next();
MultivaluedMap<String, String> headers = part.getHeaders();
String fileName = null;
if (!headers.containsKey("Content-Disposition")) {
continue;
} else {
// get the file name here
fileName = parseFileName(headers.getFirst("Content-Disposition"));
}
//.....
}
//......
}
private String parseFileName(String disposition) {
int fileNameIndex = disposition.indexOf("filename=");
if (fileNameIndex < 0) {
return null;
}
int start = disposition.indexOf("\"", fileNameIndex) + 1;
int end = disposition.indexOf("\"", start);
return disposition.substring(start, end);
}
As Stephen C said a filter can be used to get the right encoding. We had this problem on JBOSS 7.1.1 and implemented a filter.
In web xml
Filter class:
}