【发布时间】:2016-03-08 09:46:34
【问题描述】:
我有一个发送多部分表单数据的休息客户端。我将图像作为“应用程序/八位字节流”发送。图片类型为JPEG。
我应该如何在 REST 服务中正确接收这个?
目前我收到的是InputStream。
我将此输入流转换为文件,但无法打开它。尝试打开时显示error in jpeg。
文件转换逻辑的输入流
File image=File.createTempFile("image", ".JPEG");
FileUtils.copyInputStreamToFile(inputStream, image);
为了清楚起见,我将共享其余客户端存根和其余服务实现。
休息客户端存根
public class ImageTest
{
public static void main(String[] args) throws IOException
{
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target("http://localhost:8080/rest/AS/uploadreceipt");
MultipartFormDataOutput formData = new MultipartFormDataOutput();
Map<String, Object> json = new HashMap<>();
json.put("loyaltyId", "23");
formData.addFormData("json", json, MediaType.APPLICATION_JSON_TYPE);
FileInputStream fis = new FileInputStream(new File("/root/Downloads/index.jpeg"));
formData.addFormData("image", fis, MediaType.APPLICATION_OCTET_STREAM_TYPE);
Entity<MultipartFormDataOutput> entity = Entity.entity(formData, MediaType.MULTIPART_FORM_DATA);
Response response = target.request().post(entity);
}
休息服务处理
Map<String, Object> json = receiptUploadRequest.getFormDataPart("json", new GenericType<Map<String, Object>>() {});
InputStream image = receiptUploadRequest.getFormDataPart("image", new GenericType<InputStream>() {});
有什么我需要考虑的,比如标题等。因为它作为八位字节流从休息客户端发送。某些东西阻止了创建文件。。谁能帮我转换从发送的图像将客户端存根保存到文件....
【问题讨论】:
-
首先,如果您希望将整个文件内容包含在表单数据中,请不要过早读取文件的第一个字节。
-
嗨 john..我怎样才能停止过早地读取第一个字节?
-
在这种情况下,您避免在客户端的
main()中调用fis.read()(或任何其他从流中读取数据的方法)。 -
删除了 fis.read().. 还是一样的错误:|
-
约翰..你能帮忙吗..
标签: java image file inputstream resteasy