Java 如何让resteasy MultipartFormDataInput使用UTF-8解码字符串?

Java 如何让resteasy MultipartFormDataInput使用UTF-8解码字符串?,java,tomcat,jboss,resteasy,Java,Tomcat,Jboss,Resteasy,我使用的是resteasy 2.3.4-Final,在接受多部分/表单数据的调用中存在UTF-8问题。我的API的消费者是iOS和Android设备。发送的任何字符串参数都不包含字符集,因此resteasy似乎正在使用us ascii编码对字符串进行解码。我已经做了大量的工作来修复从db层到创建一个过滤器所涉及的一切,该过滤器将强制字符编码为utf-8。这解决了所有表单url编码帖子的问题,但现在两个调用仍然不起作用,它们都是多部分/表单数据调用。我知道用户应该在消息部分发送utf-8字符集,但

我使用的是resteasy 2.3.4-Final,在接受多部分/表单数据的调用中存在UTF-8问题。我的API的消费者是iOS和Android设备。发送的任何字符串参数都不包含字符集,因此resteasy似乎正在使用us ascii编码对字符串进行解码。我已经做了大量的工作来修复从db层到创建一个过滤器所涉及的一切,该过滤器将强制字符编码为utf-8。这解决了所有表单url编码帖子的问题,但现在两个调用仍然不起作用,它们都是多部分/表单数据调用。我知道用户应该在消息部分发送utf-8字符集,但我正在试图找出是否有任何方法可以强制所有内容都暂时使用utf-8解码,因为苹果需要大约2周的时间才能批准对我们的应用程序进行更新,这并不理想,但我们可能不得不在这一点上咬紧牙关。以前有没有人这样做过,并且成功地上传了多部分表单


谢谢

根据RESTEasy文档,应该可以覆盖默认内容类型:


根据RESTEasy文档,应该可以覆盖默认内容类型:


下面是一个解决编码问题的方法。也请投赞成票 臭虫

例如:

import org.apache.james.mime4j.message.BodyPart;
import org.apache.james.mime4j.message.SingleBody;
import org.jboss.resteasy.plugins.providers.multipart.InputPart;

    ...

@POST
@Path("/uploadContacts")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void uploadContacts(MultipartFormDataInput input) throws Exception {

    List<InputPart> inputParts = uploadForm.get("uploadFieldName");
    for (InputPart inputPart : inputParts) {
               // bytes extracted
               byte[] fileBytes = readByteArray(inputPart);
               // now we can read it with right encoding
               InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(fileBytes), "UTF-8");

               ...
    }
}

private byte[] readByteArray(InputPart inputPart) throws Exception {
    Field f = inputPart.getClass().getDeclaredField("bodyPart");
    f.setAccessible(true);
    BodyPart bodyPart = (BodyPart) f.get(inputPart);
    SingleBody body = (SingleBody)bodyPart.getBody();

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    body.writeTo(os);
    byte[] fileBytes = os.toByteArray();
    return fileBytes;
}
@Provider
public class MyContentTypeFilter implements ContainerRequestFilter {
  @Context
  private HttpServletRequest servletRequest;

  @Override
  public void filter(ContainerRequestContext requestContext) throws IOException {
    servletRequest.setAttribute(InputPart.DEFAULT_CONTENT_TYPE_PROPERTY, "text/plain; charset=UTF-8");
  }
}
import org.apache.james.mime4j.message.BodyPart;
导入org.apache.james.mime4j.message.SingleBody;
导入org.jboss.resteasy.plugins.providers.multipart.InputPart;
...
@职位
@路径(“/uploadContacts”)
@使用(MediaType.MULTIPART\u FORM\u数据)
public void uploadContacts(MultipartFormDataInput)引发异常{
List inputParts=uploadForm.get(“uploadFieldName”);
for(InputPart InputPart:inputParts){
//提取的字节数
byte[]fileBytes=readByteArray(inputPart);
//现在我们可以用正确的编码读取它
InputStreamReader reader=新的InputStreamReader(新的ByteArrayInputStream(文件字节),“UTF-8”);
...
}
}
私有字节[]readByteArray(InputPart InputPart)引发异常{
字段f=inputPart.getClass().getDeclaredField(“bodyPart”);
f、 setAccessible(true);
BodyPart BodyPart=(BodyPart)f.get(inputPart);
SingleBody body=(SingleBody)bodyPart.getBody();
ByteArrayOutputStream os=新建ByteArrayOutputStream();
书面形式(os);
byte[]fileBytes=os.toByteArray();
返回文件字节;
}

这里是一个解决编码问题的方法。也请投赞成票 臭虫

例如:

import org.apache.james.mime4j.message.BodyPart;
import org.apache.james.mime4j.message.SingleBody;
import org.jboss.resteasy.plugins.providers.multipart.InputPart;

    ...

@POST
@Path("/uploadContacts")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void uploadContacts(MultipartFormDataInput input) throws Exception {

    List<InputPart> inputParts = uploadForm.get("uploadFieldName");
    for (InputPart inputPart : inputParts) {
               // bytes extracted
               byte[] fileBytes = readByteArray(inputPart);
               // now we can read it with right encoding
               InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(fileBytes), "UTF-8");

               ...
    }
}

private byte[] readByteArray(InputPart inputPart) throws Exception {
    Field f = inputPart.getClass().getDeclaredField("bodyPart");
    f.setAccessible(true);
    BodyPart bodyPart = (BodyPart) f.get(inputPart);
    SingleBody body = (SingleBody)bodyPart.getBody();

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    body.writeTo(os);
    byte[] fileBytes = os.toByteArray();
    return fileBytes;
}
@Provider
public class MyContentTypeFilter implements ContainerRequestFilter {
  @Context
  private HttpServletRequest servletRequest;

  @Override
  public void filter(ContainerRequestContext requestContext) throws IOException {
    servletRequest.setAttribute(InputPart.DEFAULT_CONTENT_TYPE_PROPERTY, "text/plain; charset=UTF-8");
  }
}
import org.apache.james.mime4j.message.BodyPart;
导入org.apache.james.mime4j.message.SingleBody;
导入org.jboss.resteasy.plugins.providers.multipart.InputPart;
...
@职位
@路径(“/uploadContacts”)
@使用(MediaType.MULTIPART\u FORM\u数据)
public void uploadContacts(MultipartFormDataInput)引发异常{
List inputParts=uploadForm.get(“uploadFieldName”);
for(InputPart InputPart:inputParts){
//提取的字节数
byte[]fileBytes=readByteArray(inputPart);
//现在我们可以用正确的编码读取它
InputStreamReader reader=新的InputStreamReader(新的ByteArrayInputStream(文件字节),“UTF-8”);
...
}
}
私有字节[]readByteArray(InputPart InputPart)引发异常{
字段f=inputPart.getClass().getDeclaredField(“bodyPart”);
f、 setAccessible(true);
BodyPart BodyPart=(BodyPart)f.get(inputPart);
SingleBody body=(SingleBody)bodyPart.getBody();
ByteArrayOutputStream os=新建ByteArrayOutputStream();
书面形式(os);
byte[]fileBytes=os.toByteArray();
返回文件字节;
}

由于
org.jboss.resteasy.spi.interception.preprocessinterceptior
已被弃用,我使用带有“注入式”HttpServletRequest的
javax.ws.rs.ContainerRequestFilter
解决了这个问题

例如:

import org.apache.james.mime4j.message.BodyPart;
import org.apache.james.mime4j.message.SingleBody;
import org.jboss.resteasy.plugins.providers.multipart.InputPart;

    ...

@POST
@Path("/uploadContacts")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void uploadContacts(MultipartFormDataInput input) throws Exception {

    List<InputPart> inputParts = uploadForm.get("uploadFieldName");
    for (InputPart inputPart : inputParts) {
               // bytes extracted
               byte[] fileBytes = readByteArray(inputPart);
               // now we can read it with right encoding
               InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(fileBytes), "UTF-8");

               ...
    }
}

private byte[] readByteArray(InputPart inputPart) throws Exception {
    Field f = inputPart.getClass().getDeclaredField("bodyPart");
    f.setAccessible(true);
    BodyPart bodyPart = (BodyPart) f.get(inputPart);
    SingleBody body = (SingleBody)bodyPart.getBody();

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    body.writeTo(os);
    byte[] fileBytes = os.toByteArray();
    return fileBytes;
}
@Provider
public class MyContentTypeFilter implements ContainerRequestFilter {
  @Context
  private HttpServletRequest servletRequest;

  @Override
  public void filter(ContainerRequestContext requestContext) throws IOException {
    servletRequest.setAttribute(InputPart.DEFAULT_CONTENT_TYPE_PROPERTY, "text/plain; charset=UTF-8");
  }
}

由于
org.jboss.resteasy.spi.interception.PreProcessInterceptor
已被弃用,我使用带有“注入式”HttpServletRequest的
javax.ws.rs.ContainerRequestFilter
解决了这个问题

例如:

import org.apache.james.mime4j.message.BodyPart;
import org.apache.james.mime4j.message.SingleBody;
import org.jboss.resteasy.plugins.providers.multipart.InputPart;

    ...

@POST
@Path("/uploadContacts")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void uploadContacts(MultipartFormDataInput input) throws Exception {

    List<InputPart> inputParts = uploadForm.get("uploadFieldName");
    for (InputPart inputPart : inputParts) {
               // bytes extracted
               byte[] fileBytes = readByteArray(inputPart);
               // now we can read it with right encoding
               InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(fileBytes), "UTF-8");

               ...
    }
}

private byte[] readByteArray(InputPart inputPart) throws Exception {
    Field f = inputPart.getClass().getDeclaredField("bodyPart");
    f.setAccessible(true);
    BodyPart bodyPart = (BodyPart) f.get(inputPart);
    SingleBody body = (SingleBody)bodyPart.getBody();

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    body.writeTo(os);
    byte[] fileBytes = os.toByteArray();
    return fileBytes;
}
@Provider
public class MyContentTypeFilter implements ContainerRequestFilter {
  @Context
  private HttpServletRequest servletRequest;

  @Override
  public void filter(ContainerRequestContext requestContext) throws IOException {
    servletRequest.setAttribute(InputPart.DEFAULT_CONTENT_TYPE_PROPERTY, "text/plain; charset=UTF-8");
  }
}

难道你不能只做
byte[]data=inputPart.getBody(byte[].class,null)?您不能只执行
字节[]数据=inputPart.getBody(字节[].class,null)