1

resteasy 2.3.4-Final を使用していますが、multipart/form-data を受け入れる呼び出しで UTF-8 の問題が発生しています。私の API のコンシューマーは、iOS および Android デバイスです。送信される文字列パラメーターには文字セットが含まれていないため、resteasy は us-ascii エンコーディングで文字列をデコードしているようです。文字エンコーディングを utf-8 に強制するフィルターを作成するために、db レイヤーから関連する他のすべてを修正するために多くの作業を行いました。これにより、form-url でエンコードされたすべての POST の問題が解決されましたが、2 つの呼び出しがまだ機能せず、両方とも multipart/form-data 呼び出しになっています。消費者がメッセージ部分で utf-8 文字セットを送信する必要があることは理解していますが、存在するかどうかを把握しようとしています ' Apple がアプリケーションの更新を承認するのに約 2 週間かかるため、一時的に UTF-8 を使用してすべてを強制的にデコードする方法はありません。誰かがこれを以前に行い、マルチパートフォームのアップロードで成功しましたか?

ありがとう!

4

3 に答える 3

2

RESTEasy のドキュメントによると、デフォルトのコンテンツ タイプをオーバーライドできるはずです。

http://docs.jboss.org/resteasy/docs/2.3.4.Final/userguide/html_single/index.html#multipart_overwrite_default_content_type

import org.jboss.resteasy.plugins.providers.multipart.InputPart;

@Provider
@ServerInterceptor
public class ContentTypeSetterPreProcessorInterceptor implements
        PreProcessInterceptor {

    public ServerResponse preProcess(HttpRequest request,
            ResourceMethod method) throws Failure, WebApplicationException {
        request.setAttribute(InputPart.DEFAULT_CONTENT_TYPE_PROPERTY,
                "*/*; charset=UTF-8");
        return null;
    }

}
于 2012-06-27T07:51:42.107 に答える
1

エンコーディングの問題の回避策は次のとおりです。また、このバグRESTEASY-390に投票してください。

例:

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;
}
于 2012-09-20T12:40:28.300 に答える
1

org.jboss.resteasy.spi.interception.PreProcessInterceptorは廃止されたため、「注入された」を使用して問題を解決しましjavax.ws.rs.container.ContainerRequestFilterHttpServletRequest

例:

@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");
  }
}
于 2015-03-04T16:16:44.283 に答える