How to get text/xml as UTF-8 from a multipart/form-data request with RESTeasy?

前端 未结 2 1518
慢半拍i
慢半拍i 2021-01-23 10:37

thanks for your answer, but using an InputStream instead of using getBody(...) does also not work. The code below returns the same result as the one from my original post.

相关标签:
2条回答
  • 2021-01-23 11:08

    I generally would not rely on the getBody method on InputPart. You can actually get each part as a raw input stream and read the data in yourself. Rather than relying on the framework to convert the content to a String.

    0 讨论(0)
  • 2021-01-23 11:11

    When no charset is defined in the content-type header of your HTTP request, resteasy assumes 'charset=US-ASCII'. See org.jboss.resteasy.plugins.providers.multipart.InputPart:

    /**
        * If there is a content-type header without a charset parameter, charset=US-ASCII
        * is assumed.
        * <p>
        * This can be overwritten by setting a different String value in
        * {@link org.jboss.resteasy.spi.HttpRequest#setAttribute(String, Object)}
        * with this ("resteasy.provider.multipart.inputpart.defaultCharset")
        * String`enter code here` as key. It should be done in a
        * {@link org.jboss.resteasy.spi.interception.PreProcessInterceptor}.
        * </p>
         */
    

    So, as a work-around you can do the following:

     @Provider
    @ServerInterceptor
    public class CharsetPreProcessInterceptor implements PreProcessInterceptor {
    
        @Override
        public ServerResponse preProcess(HttpRequest request, ResourceMethod method) throws Failure, WebApplicationException {
            request.setAttribute(InputPart.DEFAULT_CHARSET_PROPERTY, "charset=UTF-8");
            return null;
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题