简体   繁体   English

使用Retrofit 1.9.x发布FileInputStream

[英]POSTing an FileInputStream with Retrofit 1.9.x

I'm trying to POST an FileInputStream using Retrofit 1.9.x (I can't move to 2.0.x yet) 我正在尝试使用Retrofit 1.9.x发布POST FileInputStream(我还不能移动到2.0.x)

I've read This Post . 我已经读过这篇文章

In that post I understood that If I use a TypedInput in my interface and implement TypedInput class wrapper that handled the stream, then it should work. 在那篇文章中,我了解到,如果我在接口中使用TypedInput并实现处理该流的TypedInput类包装器,则它应该可以工作。 It was unclear if TypedInput vs TypedOutput was the answer (TypedInput sounded the most correct, the linked retrofit documentation didn't specify as far as I could tell. Also It's all moved on to 2.0) 尚不清楚答案是TypedInput与TypedOutput(TypedInput听起来最正确,据我所知,链接的改造文档未指定。此外,所有这些都移至了2.0)

To proceed - I created a class 继续-我创建了一个课程

  private class InputStreamMunger implements TypedInput {
    private InputStream is;
    private String mimeType;
    private Long fileLength;

    public InputStreamMunger(InputStream is, String mimeType, Long fileLength) {
        this.is = is;
        this.fileLength = fileLength;
        this.mimeType = mimeType;
    }

    @Override
    public String mimeType() {
        return mimeType;
    }

    @Override
    public long length() {
        return fileLength;
    }

    @Override
    public InputStream in() throws IOException {
        return is;
    }
}

My Interface: 我的界面:

@Multipart
@POST("/MrService/v1/upload/{accountId}")
Response upload(
    @Path("accountId") String accountId,
    @Part("file") TypedInput file);

Then I call it 那我叫它

    FileInputStream is = new FileInputStream("src/test/java/com/me/MrService/tester.txt");
    InputStreamMunger file ;

    try {
        file = new InputStreamMunger(is, "text/plain", is.getChannel().size());
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }

    Response r = client.upload("12345", file );

The error i get is: 我得到的错误是:

retrofit.RetrofitError: com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class com.me.MrService.IntegrationTestIT$InputStreamMunger and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) )

Does this mean that I need to create my own mapper to handle a Stream? 这是否意味着我需要创建自己的映射器来处理Stream? I'm hoping that I'm just doing something wrong and that I don't need to jump through that hoop. 我希望我只是做错了什么,我不需要跳过那一圈。

Thanks! 谢谢!

In the end I did implement the TypedOutput instead of the TypedInput. 最后,我确实实现了TypedOutput而不是TypedInput。

private class InputStreamMunger implements TypedOutput {

    private InputStream is;
    private String mimeType;
    private Long fileLength;
    private String fileName;
    private static final int BUFFER_SIZE = 4096;


    public InputStreamMunger(InputStream is, String mimeType, Long fileLength,
            String fileName) {
        this.is = is;
        this.fileLength = fileLength;
        this.mimeType = mimeType;
        this.fileName = fileName;
    }

    @Override
    public String mimeType() {
        return mimeType;
    }

    @Override
    public long length() {
        return fileLength;
    }

    @Override
    public void writeTo(OutputStream out) throws IOException {
        byte[] buffer = new byte[BUFFER_SIZE];
        try {
            int read;
            while ((read = is.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
        } finally {
            is.close();
        }
    }

    public String fileName() {
        return fileName;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM