I faced some difficulties to upload file with non ASCII file name.
Part of the code is:
package org.example;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.MediaType;
import java.util.List;
import java.util.Map;
import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;
import org.jboss.resteasy.plugins.providers.multipart.InputPart;
import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
@Path("/hello")
public class ExampleResource {
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void hello(@MultipartForm MultipartFormDataInput formDataInput) {
Map<String, List<InputPart>> dataMap = formDataInput.getFormDataMap();
List<InputPart> inputs = dataMap.get("attachment");
System.out.println("Data map: " + dataMap);
System.out.println("Data inputs: " + inputs);
}
}
Step to reproduce:
-
Create both files with different names and one extension to compare:
Новый_документ.docx
andNew_document.docx
. -
Once both are created try to upload it using postman with
attachment
key
- Debug the data and have a look that
formDataInput.getFormDataMap()
method calleddataMap
returns really strange key that is encoded with UTF-8 and it’s cut because it has.doc
instead of.docx
. I suppose the key of the map isfilename*=UTF-8
value that is taken fromContent-Disposition
header.
Content-Disposition: form-data; name="attachment"; filename="Новый_документ.docx"; filename*=UTF-8''%D0%9D%D0%BE%D0%B2%D1%8B%D0%B9_%D0%B4%D0%BE%D0%BA%D1%83%D0%BC%D0%B5%D0%BD%D1%82.docx
Inputs
is null.
- Do the same with ASCII file name
New_document.docx
. And pay attention at the same map. For now there is expected key andInputs
isn’t null
So, my problem is: when I try to upload any file with non ASCII file name I can’t get attachment
key from formDataInput.getFormDataMap()
method result.
I tried to use several approaches to solve it:
- Was added
@Consumes(MediaType.MULTIPART_FORM_DATA + ";charset=utf-8")
instead of
@Consumes(MediaType.MULTIPART_FORM_DATA)
- Was added ContainerRequestFilter implementation:
package org.example;
import static org.jboss.resteasy.plugins.providers.multipart.InputPart.DEFAULT_CHARSET_PROPERTY;
import static org.jboss.resteasy.plugins.providers.multipart.InputPart.DEFAULT_CONTENT_TYPE_PROPERTY;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerRequestFilter;
import jakarta.ws.rs.ext.Provider;
import java.io.IOException;
@Provider
public class ContainerRequestFilterImpl implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
requestContext.setProperty(DEFAULT_CONTENT_TYPE_PROPERTY, "multipart/form-data;charset=utf-8");
requestContext.setProperty(DEFAULT_CHARSET_PROPERTY, "utf-8");
}
}
I have no idea already what I’ve done wrong and how to solve it
Used:
io.quarkus:quarkus-bom:3.13.0
org.jboss.resteasy:resteasy-multipart-provider:6.2.9.Final