【问题标题】:Encode filename multipart/form-data on wildfly 9在 Wildfly 9 上编码文件名 multipart/form-data
【发布时间】: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
  • 听起来你已经知道了!或许你忽略了:How can I get resteasy MultipartFormDataInput to decode strings using UTF-8?
  • 我想高兴地哭泣。带有反射的黑客对我有帮助。 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


【解决方案1】:

我将 Wildfly 9 与 resteasy 一起使用。上面的代码对我来说导致类转换异常。下面的代码解决了我的问题:

        Field field = inputPart.getClass().getDeclaredField("bodyPart");
        field.setAccessible(true);
        Object bodyPart = field.get(inputPart);
        Method methodBodyPart = bodyPart.getClass().getMethod("getHeader", new Class[]{});
        Iterable iterable = (Iterable)methodBodyPart.invoke(bodyPart, new Class[]{});
        Object[] content = IteratorUtils.toArray(iterable.iterator());
        Method methodContent = content[0].getClass().getMethod("getRaw", new Class[]{});

        String[] contentDisposition = methodContent.invoke(content[0], new Class[]{}).toString().split(";");

        for (String filename : contentDisposition) {
            if ((filename.trim().startsWith("filename"))) {

                String[] name = filename.split("=");

                String finalFileName = name[1].trim().replaceAll("\"", "");
                return finalFileName;
            }
        }

【讨论】:

  • 这个解决方法也适用于 Wildfly 11!谢谢!
猜你喜欢
  • 2010-10-07
  • 2021-11-21
  • 1970-01-01
  • 1970-01-01
  • 2015-10-23
  • 1970-01-01
  • 1970-01-01
  • 2018-12-28
  • 2016-01-27
相关资源
最近更新 更多