如何通过编程方式设置内容类型?获取org.jvnet.mimepull.MIMEParsingException:缺少开始边界

时间:2019-06-20 10:43:18

标签: java rest jersey

我有一个命令行Java实用程序,需要对jersey rest服务进行rest调用。

@POST    
@Path("/{name}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response insert(@PathParam("name") String nestName,
                           @QueryParam("id") String id,
                           @FormDataParam("file") InputStream fileStream,
                           @FormDataParam("file") FormDataContentDisposition fileDetail) throws IOException {}

并通过如下所示的java命令行实用程序发出请求:

Map<String,String> headers = new HashMap<String,String>();
headers.put(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA + "; boundary=--------------------------458269817398405552698839");
headers.put(HttpHeaders.ACCEPT, "*/*");
headers.put(HttpHeaders.CACHE_CONTROL, "no-cache");
headers.put(HttpHeaders.ACCEPT_ENCODING, "gzip, deflate");

URLConnection conn = url.openConnection();
if(headers!=null && !headers.isEmpty()) {
    for(Map.Entry<String, String> entry: headers.entrySet()) {
        conn.setRequestProperty(entry.getKey(),entry.getValue());
    }
}

ServiceHttpResponse response = new ServiceHttpResponse(con.getResponseCode(), con.getResponseMessage());

我收到以下异常。还有其他设置mime类型的方法吗?

  

javax.ws.rs.BadRequestException:HTTP 400错误请求

     在

  org.glassfish.jersey.media.multipart.internal.MultiPartReaderClientSide.readFrom(MultiPartReaderClientSide.java:188)     在   org.glassfish.jersey.media.multipart.internal.MultiPartReaderClientSide.readFrom(MultiPartReaderClientSide.java:93)     在   org.glassfish.jersey.message.internal.ReaderInterceptorExecutor $ TerminalReaderInterceptor.invokeReadFrom(ReaderInterceptorExecutor.java:256)     在   org.glassfish.jersey.message.internal.ReaderInterceptorExecutor $ TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:235)     在   org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:155)     在   org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundReadFrom(MappableExceptionWrapperInterceptor.java:74)     在   org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:155)     在   org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1085)     在   org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:874)     在   org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:808)     在   org.glassfish.jersey.server.ContainerRequest.readEntity(ContainerRequest.java:234)     在   org.glassfish.jersey.media.multipart.internal.FormDataParamValueFactoryProvider $ ValueFactory.getEntity(FormDataParamValueFactoryProvider.java:126)     在   org.glassfish.jersey.media.multipart.internal.FormDataParamValueFactoryProvider $ FormDataParamValueFactory.provide(FormDataParamValueFactoryProvider.java:280)     在   org.glassfish.jersey.server.spi.internal.ParamValueFactoryWithSource.provide(ParamValueFactoryWithSource.java:71)     在   org.glassfish.jersey.server.spi.internal.ParameterValueHelper.getParameterValues(ParameterValueHelper.java:90)

     在

  org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider $ AbstractMethodParamInvoker.getParamValues(JavaResourceMethodDispatcherProvider.java:127)     在   org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider $ ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:160)

     在

  org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)

     在

  org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)

     在

  org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)

     在

  org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)

     在

  org.glassfish.jersey.server.ServerRuntime $ 2.run(ServerRuntime.java:326)

     

在org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:271)

     

在org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:267)

     

在org.glassfish.jersey.internal.Errors.process(Errors.java:315)

     

at org.glassfish.jersey.internal.Errors.process(Errors.java:297)   org.glassfish.jersey.internal.Errors.process(Errors.java:267)

     在

  org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)     在   org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)

     在

  org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)

     在

  org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)     在   org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)

     在

  org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)

     在

  org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)

     在

  org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)

     在

  org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:808)

     在

  org.eclipse.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1669)

     在

  com.cadence.adw.common.generic.xml.server.service.http.RequestInterceptor.doFilter(RequestInterceptor.java:95)

     在

  org.eclipse.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1652)

     在

  org.eclipse.jetty.servlets.UserAgentFilter.doFilter(UserAgentFilter.java:83)     在   org.eclipse.jetty.servlets.GzipFilter.doFilter(GzipFilter.java:300)

     在

  org.eclipse.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1652)     在   org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585)

     在

  org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:221)

     在

  org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)

     在

  org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)

     在

  org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)     在   org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061)

     在

  org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)

     在

  org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:215)

     在

  org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:110)

     在

  org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)

     

在org.eclipse.jetty.server.Server.handle(Server.java:497)

     org.eclipse.jetty.server.HttpChannel.handle上的

(HttpChannel.java:310)     在   org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257)

     在

  org.eclipse.jetty.io.AbstractConnection $ 2.run(AbstractConnection.java:540)

     在

  org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)

     在

  org.eclipse.jetty.util.thread.QueuedThreadPool $ 3.run(QueuedThreadPool.java:555)

     

在java.lang.Thread.run(Thread.java:748)

     

原因:org.jvnet.mimepull.MIMEParsingException:缺少开始   边界

     

在org.jvnet.mimepull.MIMEParser.skipPreamble(MIMEParser.java:318)

     

在org.jvnet.mimepull.MIMEParser.access $ 300(MIMEParser.java:68)

     在

  org.jvnet.mimepull.MIMEParser $ MIMEEventIterator.next(MIMEParser.java:154)

     在

  org.jvnet.mimepull.MIMEParser $ MIMEEventIterator.next(MIMEParser.java:132)

     

在org.jvnet.mimepull.MIMEMessage.makeProgress(MIMEMessage.java:228)     在org.jvnet.mimepull.MIMEMessage.parseAll(MIMEMessage.java:189)

     在

  org.jvnet.mimepull.MIMEMessage.getAttachments(MIMEMessage.java:115)

     在

  org.glassfish.jersey.media.multipart.internal.MultiPartReaderClientSide.getMimeParts(MultiPartReaderClientSide.java:272)

     在

  org.glassfish.jersey.media.multipart.internal.MultiPartReaderClientSide.readMultiPart(MultiPartReaderClientSide.java:231)

     在

  org.glassfish.jersey.media.multipart.internal.MultiPartReaderServerSide.readMultiPart(MultiPartReaderServerSide.java:91)

     在

  org.glassfish.jersey.media.multipart.internal.MultiPartReaderClientSide.readFrom(MultiPartReaderClientSide.java:183)

     

...另外58个

1 个答案:

答案 0 :(得分:2)

设置请求参数不是发送多部分请求的正确方法。仅将内容类型设置为边界是不够的。该边界实际上是在请求的主体内使用的,以分隔请求的不同“部分”并结束主体。例如

Content-Type: multipart/form-data; boundary=AaB03x

--AaB03x
Content-Disposition: form-data; name="submit-name"

Larry
--AaB03x
Content-Disposition: form-data; name="files"; filename="file1.txt"
Content-Type: text/plain

... contents of file1.txt ...
--AaB03x--

UrlConnection(或HttpUrlConnection没有任何API可以轻松发送Multipart请求。您需要手动创建此请求正文并通过连接的输出流发送出去。

multipart的目的是发送多个参数,其中一些参数是二进制对象,例如图像文件等。如果所有参数都是文本,则可以使用application/x-www-form-urlencoded content-type。这是在发出POST请求时在HttpUrlConnection上设置请求参数的过程。在服务器上,您将使用@FormParam("param-name")而不是@FormDataParam。如果您正在执行GET请求,则为@QueryParam

如果要发出Multipart请求,则应调查支持发送请求的客户端,而不必手动构造正确的主体格式。您可以查看Jersey Client及其Multipart support。还有许多其他客户端库。只是做些搜索。