Encoding multi-user data / form data using pojo

I am having problems with repeated and multipolar forms, and I was hoping that someone might have the same problem or if I could, I could help me.

My goal is to download the file and some parameters at the same time. I tried this using @MultipartForm annotated to a POJO form:

@PUT @Path("/userdebug1/{userId}") @Consumes("multipart/form-data") @Produces("application/json;charset=UTF-8") public String updateUserDebug1( @MultipartForm UserRequestForm request ) { return request.getName(); } 

With the form UserRequestForm is

 public class UserRequestForm { @FormParam("name") String name; @FormParam("blob") @PartType("application/octet-stream") byte[] image; public String getName() { return name; } public void setName(String n) { this.name =n; } public byte[] getImage() { return image; } public void setImage(byte[] image) { this.image = image; } } 

All this works well, except for the fact that the character encoding is broken . If I use the umlaut, it does not return properly. If I use the following method:

 @PUT @Path("/userdebug2/{userId}") @Consumes("multipart/form-data") @Produces("application/json;charset=UTF-8") public String updateUserDebug2(MultipartFormDataInput form) { try { return form.getFormDataMap().get("name").get(0).getBodyAsString(); } catch (IOException e) { e.printStackTrace(); } return "error"; } 

I used charlesproxy to send the same request to two of both urls. Here is one of them. The other differs only in the URL.

 PUT /api/v1/userdebug1/A4BE364C-15F8-59B0-87C3-DCA0A123644A HTTP/1.1 Host: localhost:8081 Content-Type: multipart/form-data; charset=utf-8; boundary=0xKhTmLbOuNdArY-5C999EAA-3828-4919-98B7-19D4FD738814 Accept-Encoding: gzip Connection: close Content-Length: 205 --0xKhTmLbOuNdArY-5C999EAA-3828-4919-98B7-19D4FD738814 Content-Disposition: form-data; name="name" Content-Type: text/plain;charset=utf-8 ü --0xKhTmLbOuNdArY-5C999EAA-3828-4919-98B7-19D4FD738814-- 

Answer 1 (using the pojo form):

   

Answer 2 (using MultipartFormDataInput):

 ü 

Any ideas? Am I doing something wrong or is this a mistake?

Thank you for the amazing stackoverflow community. You have already helped a lot. Even if this is the first time I asked a question.

+4
source share
1 answer

I have the same problem. I went through the resteasy code and found that the problem appears in org.jboss.resteasy.plugins.providers.ProviderHelper.readString(InputStream, MediaType) .

 public static String readString(InputStream in, MediaType mediaType) throws IOException { byte[] buffer = new byte[1024]; ByteArrayOutputStream builder = new ByteArrayOutputStream(); int wasRead = 0; do { wasRead = in.read(buffer, 0, 1024); if (wasRead > 0) { builder.write(buffer, 0, wasRead); } } while (wasRead > -1); byte[] bytes = builder.toByteArray(); String charset = mediaType.getParameters().get("charset"); if (charset != null) return new String(bytes, charset); else return new String(bytes, "UTF-8"); } 

The in input stream is org.jboss.resteasy.plugins.providers.multipart.MultipartInputImpl$ReaderBackedInputStream (for which I don't seem to have source code), which contains an InputStreamReader reading from a ByteArrayInputStream containing the correct UTF-8 bytes. However, when it is read, it returns invalid data. My "nér" [110, -61, -87, 114] (which is present in the backup in ) becomes [110, -23, 114] . This is then passed to new String(bytes, "UTF-8") , which is incorrect.

I hope this helps someone, I get no more than using the information I found in the above question to crack the problem using form.getFormDataMap().get("name").get(0).getBodyAsString() . Thanks for this.

+2
source

Source: https://habr.com/ru/post/1402039/


All Articles