小编典典

没有可用的MessageBodyWriter用于媒体类型“ multipart / form-data”

tomcat

我正在尝试创建一个jersey客户端,以使用多部分请求击中服务器。
我有要上传的文件作为java.io.ByteArrayOutputStream类型流。
我搜索了错误,并在这里遇到了这个问题,并register(MultiPartFeature.class)在创建时添加 了它Client,但没有效果。 我可能只需要向正确的方向轻推即可。谢谢!

我没有使用Maven或任何东西。

java文件中的相关部分是:

FormDataMultiPart multiPart = new FormDataMultiPart();
FormDataContentDisposition.FormDataContentDispositionBuilder dispositionBuilder = FormDataContentDisposition
 .name("file");

dispositionBuilder.fileName("file.zip");
FormDataContentDisposition formDataContentDisposition = dispositionBuilder.build();

/*
 * byteArrayOutputStream is of type java.io.ByteArrayOutputStream
 * and contains the file I wish to upload.
 */
multiPart.bodyPart(
 new FormDataBodyPart("file", byteArrayOutputStream, MediaType.APPLICATION_OCTET_STREAM_TYPE)
 .contentDisposition(formDataContentDisposition));

Entity < FormDataMultiPart > entity = Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE);

Client client = ClientBuilder.newClient(new ClientConfig().register(MultiPartFeature.class));
WebTarget webTarget = client.target("http://localhost:3000/path/to/service");

// send request

Invocation.Builder invocationBuilder = webTarget.request();
Response response = invocationBuilder.post(entity);
return response;

来自的相关部分web.xml是:

          <servlet>
              <servlet-name>Jersey Web Application</servlet-name>
              <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
              <init-param>
                     <param-name>jersey.config.server.provider.packages</param-name>
                     <param-value>com.example</param-value>
              </init-param>
              <init-param>
                     <param-name>jersey.config.server.provider.classnames</param-name>
                     <param-value>org.glassfish.jersey.filter.LoggingFilter;org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
              </init-param>
              <load-on-startup>1</load-on-startup>
       </servlet>

尝试在Apache Tomcat 8.5运行时服务器上的Eclipse中运行它时,出现以下错误。

SEVERE: Servlet.service() for servlet [Jersey Web Application] in context with path [/TestRest] threw exception [javax.ws.rs.ProcessingException: No available MessageBodyWriter for class "class java.io.ByteArrayOutputStream" and media type "multipart/form-data".] with root cause
java.lang.IllegalArgumentException: No available MessageBodyWriter for class "class java.io.ByteArrayOutputStream" and media type "multipart/form-data".
    at org.glassfish.jersey.media.multipart.internal.MultiPartWriter.writeTo(MultiPartWriter.java:229)
    at org.glassfish.jersey.media.multipart.internal.MultiPartWriter.writeTo(MultiPartWriter.java:79)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.invokeWriteTo(WriterInterceptorExecutor.java:265)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:250)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1130)

阅读 581

收藏
2020-06-16

共1个答案

小编典典

没有MessageBodyWriter那个可以处理的OutputStream。使用InputStream代替

2020-06-16