0

My upload rest method works fine unless the filename contains a special character:

@POST
@Consumes({ MediaType.MULTIPART_FORM_DATA })
public Response uploadFile(MultipartFormDataInput input) {
    Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
    List<InputPart> inputParts = uploadForm.get("file_upload");

    // Do Stuff...

}

The filename is Test.png, inputPart.getHeaders() = [Content-Disposition=form-data; name="tws_file"; filename="test.png",Content-Type=image/png] --> OK

The filename is Döner.png, inputPart.getHeaders() = [Content-Disposition=form-data; name="tws_file"; filename="d��ner.png",Content-Type=image/png] --> Not OK

As you can see, the "ö" becomes "��".

Things I've tried so far:

  • Setting the default encoding in my jboss_web.xml to <default-encoding>UTF-8</default-encoding> (see here)
  • Changing my @Consumes annotation to @Consumes(MediaType.MULTIPART_FORM_DATA+";charset=UTF-8"); (see here)
  • Changing the encoding in a container request filter with requestContext.setProperty(InputPart.DEFAULT_CHARSET_PROPERTY, "UTF-8"); (see here)

Nothing seems to work. Any ideas ? I am using Wildfly 11 and Resteasy-multipart-provider 3.0.24.Final

Tim
  • 3,910
  • 8
  • 45
  • 80

3 Answers3

2

I ended up using the workaround of encoding the filename with a URI Encoder manually before sending it. On the other side, I decode it. That way, special characters are not a problem anymore.

Frontend in Angular:

encodeURI(fileName));

Backend in Java:

URLDecoder.decode(fileName, "UTF-8");
Tim
  • 3,910
  • 8
  • 45
  • 80
1

Adding a javax.ws.rs.container.ContainerRequestFilter solved this issue for me (Wildfly 11.0.0, Resteasy provided):

@Provider 
public class CharsetRequestFilter implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        requestContext.setProperty(InputPart.DEFAULT_CHARSET_PROPERTY, "UTF-8");
    }

}
Anton Goncharov
  • 363
  • 3
  • 11
0

The problem has been fixed in the latest version: https://issues.jboss.org/browse/RESTEASY-1779

Tim
  • 3,910
  • 8
  • 45
  • 80