【问题标题】:Displaying file: MessageBodyReader not found for media type=application/octet-stream显示文件:找不到媒体类型 = 应用程序/八位字节流的 MessageBodyReader
【发布时间】:2015-03-27 16:53:27
【问题描述】:

我的要求是我应该使用 RESTFul 服务显示一个文件。这是我如何进行的:

服务器:

@GET
@Path("/{name}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getFile {
    ...    
   return Response.ok(inputStream).header("Content-Disposition", "attachment; filename=" + fileName).build();

客户:

final WebTarget target = createRestClient("path/" + fileName, new HashMap<String, Object>());
return target.request(MediaType.APPLICATION_OCTET_STREAM).get().readEntity(Part.class);

当我运行它时,我得到了这个错误:

MessageBodyReader not found for media type=application/octet-stream, type=interface javax.servlet.http.Part, genericType=interface javax.servlet.http.Part.

你知道这是从哪里来的吗?

谢谢。

【问题讨论】:

    标签: web-services jersey jax-rs


    【解决方案1】:

    javax.servlet.http.Part 应该用于获取上传的多部分数据,并且由您从HttpServletRequest 获取的servlet 容器创建。它不应该以这种方式使用。旁边的数据甚至不是多部分的。

    相反,您可以简单地从Response 中获取InputStream,并从标题中显式获取Content-Dispostion。类似的东西

    Response response = target.request()
            .accept(MediaType.APPLICATION_OCTET_STREAM)
            .get();
    // get InputStream
    InputStream is = response.readEntity(InputStream.class);
    // get Content-Disposition header
    String contentDisposition = (String)response
            .getHeaderString(HttpHeaders.CONTENT_DISPOSITION);
    // get filename
    contentDisposition = contentDisposition
            .substring(contentDisposition.indexOf("filename=") + "filename".length() + 1);
    System.out.println(contentDisposition);
    

    【讨论】:

      猜你喜欢
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 2014-06-19
      • 2015-01-21
      • 2015-08-11
      • 2023-02-02
      • 2022-10-11
      • 2016-05-27
      相关资源
      最近更新 更多