如何在 rest 服务中接收以 octet-stream 类型发送的图像?
How to receive an image send as octet-stream type in the rest service?
我有一个发送多部分表单数据的休息客户端。我发送的图像为 "application/octet-stream"。图片类型为JPEG。
我应该如何在 REST 服务中正确接收它?
目前我收到的是InputStream
。
我将此输入流转换为文件,但我无法打开 it.It 在尝试打开它时说 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>() {});
有什么我需要考虑的吗,比如 headers,等等..因为它作为 octet-stream 从 rest client.something 发送会阻止创建文件..任何人都可以帮我将从 rest 客户端存根发送的图像转换为文件...
不要调用 fis.read()
,因为它会占用图像流的第一个字节。
也许您应该在问题中包含将客户端输入流转换为文件的代码。
我将输入流对象设置为相应的 pojo field.This 导致输入流损坏。
因此,在设置为 pojo 字段之前,我将输入流转换为 file.The 现在创建的文件是完美的。
我有一个发送多部分表单数据的休息客户端。我发送的图像为 "application/octet-stream"。图片类型为JPEG。
我应该如何在 REST 服务中正确接收它?
目前我收到的是InputStream
。
我将此输入流转换为文件,但我无法打开 it.It 在尝试打开它时说 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>() {});
有什么我需要考虑的吗,比如 headers,等等..因为它作为 octet-stream 从 rest client.something 发送会阻止创建文件..任何人都可以帮我将从 rest 客户端存根发送的图像转换为文件...
不要调用 fis.read()
,因为它会占用图像流的第一个字节。
也许您应该在问题中包含将客户端输入流转换为文件的代码。
我将输入流对象设置为相应的 pojo field.This 导致输入流损坏。 因此,在设置为 pojo 字段之前,我将输入流转换为 file.The 现在创建的文件是完美的。