【问题标题】:How to receive an image send as octet-stream type in the rest service?如何在其余服务中接收以八位字节流类型发送的图像?
【发布时间】: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


【解决方案1】:

不要调用fis.read(),因为它正在消耗图像流的第一个字节。

也许您应该在问题中包含将客户端输入流转换为文件的代码。

【讨论】:

  • 我已经在上面加上了anducs...文件转换逻辑标题
  • 如何将InputStream image 转换为文件?
  • 文件图像=File.createTempFile("image", ".JPEG"); FileUtils.copyInputStreamToFile(inputStream, image);
【解决方案2】:

我将输入流对象设置为相应的 pojo 字段。这导致输入流损坏。 因此,在设置为 pojo 字段之前,我将输入流转换为文件。创建的文件现在很完美。

【讨论】:

    猜你喜欢
    • 2016-10-19
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 2019-02-18
    • 2016-09-27
    • 2015-06-01
    • 2022-08-20
    • 1970-01-01
    相关资源
    最近更新 更多