如何在 REST Web 服务中接收发布的图像 [英] How to receive a POSTed image in a REST web service

查看:44
本文介绍了如何在 REST Web 服务中接收发布的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

公开可以接受 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?

推荐答案

首先,您希望以 InputStream 的形式接收图像,因为它们可能非常大.(我有一台可生成 12 兆像素图像的数码相机.它不是最新型号.即使存储为 JPEG,它生成的图像也有相当多的兆字节.您不希望这种数据混乱超过内存必要的.)

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.)

其次,您可以让您的网络服务接受未编码的原始数据作为上传.它实际上比采用 base-64 编码数据(假设您将其作为流提取)更容易,并且可以很好地节省系统带宽和处理.如果您想同时发送其他数据,您应该考虑处理多部分内容.(唉,我对那部分没有经验;我一直在研究的 RESTful Web 服务不需要它.)

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.)

第三,图像应该被描述为 image/* 空间中的一种内容类型,但在实践中仔细检查,看看这是否是你真正得到的.在将它们返回给客户时,您必须将其作为 image/* 集合之一发送回去.

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 the image/* set.

这篇关于如何在 REST Web 服务中接收发布的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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