如何在 REST Web 服务中接收 POSTed 图像
公开可接受 Base64 编码图像的基于 REST 的 Web 服务的一般方法是什么。我还不确定图像是 InputStream
还是 ByteArray
。我正在使用RESTEasy。我可以将其映射为 @FormParam
(javax.ws.rs.FormParam
) 吗?传入图像的常用数据类型是什么?
What is the general approach to exposing a REST based web service that can accept a base64 encoded image. I'm not sure yet if the image will be an InputStream
or a ByteArray
. I'm using RESTEasy. Can I just map this as a @FormParam
(javax.ws.rs.FormParam
)? What is the usual data type for the incoming image?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您希望以
InputStream
的形式接收图像,因为它们可能非常大。 (我有一台可以生成 12 兆像素图像的数码相机。这不是最新型号。即使以 JPEG 格式存储,它生成的图像也有相当多兆字节。您不希望此类数据占用内存超过必要。)其次,您可以使您的网络服务接受原始未编码数据作为上传。它实际上比获取 Base-64 编码数据更容易(假设您将其作为流拉取),并且可以很好地节省系统带宽和处理能力。如果您想同时发送其他数据,您应该考虑处理多部分内容。 (唉,我对这部分没有经验;我一直在开发的 RESTful Web 服务不需要它。)
第三,图像应该被描述为
中的内容类型>image/*
空间,但在实践中请仔细检查,看看这是否是您真正得到的。将它们返回给客户端时,您必须将其作为image/*
集之一发送回。First off, you want to receive the image as an
InputStream
because they can be quite large. (I have a digital camera that produces a 12 megapixel image. It's not the latest model. The images it produces are quite a few megabytes even when stored as JPEGs. You don't want to get that sort of data cluttering up memory more than necessary.)Secondly, you can make your web service accept raw unencoded data as upload. It's actually easier than taking base-64 encoded data (provided you're pulling it as a stream) and it's a good saving of system bandwidth and processing. If you're wanting to send other data at the same time, you should consider dealing with multipart content. (Alas, I've no experience with that part; haven't needed it for the RESTful webservices I've been working on.)
Thirdly, images should be described as a content type in the
image/*
space, but double check in practice to see if that's what you really get. When serving them up back to clients, you must send it back as one of theimage/*
set.