在一个请求中上传JSON和二进制文件 [英] Uploading JSON and binary file in one request

查看:330
本文介绍了在一个请求中上传JSON和二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找创建一个RESTful API以便与Android和iOS应用程序一起使用.到目前为止,我一直在尝试在服务器上使用Jersey,然后在客户端上使用相应的http库.目前,我一直使用multipart/related作为请求的模仿类型,JSON形成主体的第一部分,然后是jpeg图像作为第二部分.

I am looking to create a RESTful API for use with an Android and iOS app. So far I have been experimenting with using Jersey on the server and then the appropriate http libraries on the client side. At the moment I have been using multipart/related as the mimetype for the request with JSON forming the first part of the body then a jpeg image as the second.

到目前为止,我在向服务器发出请求(从Jersey获得406 Not Acceptable)时遇到了问题.我注意到multipart/related主要用于发送电子邮件.

So far I have had problems with making the request to the server, getting a 406 Not Acceptable from Jersey. I note that multipart/related is primarily used in sending emails. Is there actually a way that I can support mixed type content as an upload or have I entirely mis-understood the usage of multipart/related in this context?

推荐答案

您可能希望查看此博客,以获取更多信息,但这是可以帮助您的重要部分:

You may want to look at this blog, for more information, but here is the important part to help you along:

http://www.mkyong.com /webservices/jax-rs/file-upload-example-in-jersey/

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
    @FormDataParam("file") InputStream uploadedInputStream,
    @FormDataParam("file") FormDataContentDisposition fileDetail) {

    String uploadedFileLocation = "d://uploaded/" + fileDetail.getFileName();

    // save it
    writeToFile(uploadedInputStream, uploadedFileLocation);

    String output = "File uploaded to : " + uploadedFileLocation;

    return Response.status(200).entity(output).build();
}

我希望您改用multipart/form-data,因为这是multipart/related描述的一部分:

I expect you want multipart/form-data instead, as this is part of the description of multipart/related:

Multipart/Related媒体类型适用于复合对象
由几个相互关联的身体部位组成.对于
多部分/相关对象,
无法正确显示 分别显示身体的各个组成部分.内容类型 Multipart/Related对象的类型由type参数指定.
如果指定了开始"参数,则它会通过Content-ID指向
包含对象根的主体部分.默认根是
Multipart/Related正文中的第一正文部分.

The Multipart/Related media type is intended for compound objects
consisting of several inter-related body parts. For a
Multipart/Related object, proper display cannot be achieved by
individually displaying the constituent body parts. The content-type of the Multipart/Related object is specified by the type parameter.
The "start" parameter, if given, points, via a content-ID, to the
body part that contains the object root. The default root is the
first body part within the Multipart/Related body.

有关此mime类型的更多信息,请查看

For more on this mime type you can look at

http://tools.ietf.org/html/rfc2387

这篇关于在一个请求中上传JSON和二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆