java - CXF文件上传服务: Couldn't determine the boundary from the message

标签 java rest cxf attachment

我正在制作一个非常简单的 POST 服务的原型(prototype)来上传文件:

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("fileUpload")
public Response uploadFile(MultipartBody body) {

    //never gets to here
    System.out.println("In uploadFile");

    //TODO: implementation

    return Response.ok().build();
}

我明白了

org.apache.cxf.interceptor.Fault: Couldn't determine the boundary from the message!

我还尝试将方法声明替换为:

public Response uploadFile(List<Attachment> attachments, 
    @Context HttpServletRequest request) {

根据 Google 的一些调查结果,没有任何帮助。

我使用两个不同的客户端来调用此服务:chrome://poster ,其中有一个包含文件附件的字段,以及一个简单的 Python 脚本,如所述 here 。两个客户端产生相同的结果。

我应该如何更改我的服务实现或调用或两者,以便能够通过 CXF 验证并进入服务的实际正文?

引用文献:

最佳答案

服务器端代码看起来不错。问题是您从客户端发送数据的方式。您将数据作为有效负载中的流发送,而不是作为有边界的附件发送。要快速验证,您可以通过启用 CXF 功能 LoggingFeature 或拦截器 LoggingInInterceptorLoggingoutInterceptor 来启用日志记录请求和响应。在日志中,如果您看到有效负载传入数据,那么您正在将数据作为流发送,在这种情况下,您需要更改发送数据的方式,否则您可以将消耗更改为应用程序/八位字节流并直接使用输入流接收数据。

<小时/>

我不知道您正在使用的工具,但我使用 Chrome 扩展 postman测试 REST 服务。如果您安装扩展并启动应用程序。

您可以使用以下方法上传文件。

enter image description here

  1. 从下拉列表中将方法类型更改为 POST
  2. 输入网址
  3. 选择选项卡正文
  4. 选择表单数据单选按钮
  5. 在最右侧的行中,从下拉列表中选择文件。如图所示。
  6. 选择要上传的文件。
  7. 可选输入多部分 key 。
  8. 最后点击“发送”。
<小时/>

我们可以通过选择二进制单选按钮并上传文件来重现您的错误,如下所示。

enter image description here

关于java - CXF文件上传服务: Couldn't determine the boundary from the message,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41580304/

相关文章:

spring-boot - WebLogic 12c 上的 Apache CXF SOAP JAXB 问题

java - 在loadrunner 9.52中使用java over WEB(http/html)协议(protocol)

java - 如何在JSP中显示对象的数据

node.js - 从 REST Api (Node.js) 中调用 REST Api

ssl - 等待关闭时出现异常 javax.net.ssl.SSLHandshakeException :

java - Apache CXF : get SOAP message inside service method

java - Java中如何找到多个Map的共同部分?

Java-Spring-JSP 下载 xls 文件 - POST 正常,GET 不起作用

javascript - $scope 不从表单输入中获取字符串 - AngularJS,JS

Spring Security 与 REST 服务的 SAML token