扩展的 MultipartEntity 无法正确写出流

发布于 2024-11-16 12:01:28 字数 1769 浏览 5 评论 0原文

我想在我的 AndroidHttpClient 中实现一个 ProgressDialog。我在这里找到了一个简单的实现 CountingMultipartEntity.
另外我添加了内容长度支持。我重写了 addPart 方法。
FileBody 上传几乎可以正常工作。当上传包含​​一个文件时,它工作正常,但当有两个文件时,第二个文件仅上传部分。
InputStreamBody 可以工作,但仅当我不计算InputStream 的长度时才有效。所以我必须重新设置它,但是如何呢?

这里是我最重要的addPart

@Override
public void addPart(String name, ContentBody cb) {
    if (cb instanceof FileBody) {
        this.contentLength += ((FileBody) cb).getFile().length();
    } else if (cb instanceof InputStreamBody) {
        try {
            CountingInputStream in =
                new CountingInputStream(((InputStreamBody) cb).getInputStream());
            ObjectInputStream ois = new ObjectInputStream(in);
            ois.readObject();
            this.contentLength += in.getBytesRead();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    super.addPart(name, cb);
}

CountingInputStreamInputStream的简单扩展:

public class CountingInputStream extends InputStream {
    private InputStream source;
    private long bytesRead = 0;

    public CountingInputStream(InputStream source) {
        this.source = source;
    }

    public int read() throws IOException {
        int value = source.read();
        bytesRead++;
        return value;
    }

    public long getBytesRead() {
        return bytesRead;
    }
}

计数几乎可以工作,只有2个字节,这应该'不在那儿。但这非常重要。

首先我认为必须重置流。 in.getReadedBytes(); 之后调用的重置会导致 IOException

感谢您的任何建议。

I want to implement a ProgressDialog in my AndroidHttpClient. I found a simple implementation here CountingMultipartEntity.
Additional I added the content length support. I override the method addPart.
The FileBody upload works almost fine. When the upload contains one file it works perfect, but when there are two files the second file is only uploaded partial.
The InputStreamBody works but only when I don't count the length of the InputStream. So I have to reset it, but how?

Here my overriding addPart:

@Override
public void addPart(String name, ContentBody cb) {
    if (cb instanceof FileBody) {
        this.contentLength += ((FileBody) cb).getFile().length();
    } else if (cb instanceof InputStreamBody) {
        try {
            CountingInputStream in =
                new CountingInputStream(((InputStreamBody) cb).getInputStream());
            ObjectInputStream ois = new ObjectInputStream(in);
            ois.readObject();
            this.contentLength += in.getBytesRead();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    super.addPart(name, cb);
}

The CountingInputStream is a simple extension of the InputStream:

public class CountingInputStream extends InputStream {
    private InputStream source;
    private long bytesRead = 0;

    public CountingInputStream(InputStream source) {
        this.source = source;
    }

    public int read() throws IOException {
        int value = source.read();
        bytesRead++;
        return value;
    }

    public long getBytesRead() {
        return bytesRead;
    }
}

The counting works almost, there are only 2 bytes, which shouldn't be there. But that is so important.

First I thought the stream must be reseted. The reset called after in.getReadedBytes(); leads into an IOException.

Thanks for any advices.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮光之海 2024-11-23 12:01:28

我发现了我的错误。我重写了方法getContentLength(),该方法对于传输很重要,删除我自己的版本后文件传输工作正常。

为了获取InputStream的大小,我使用了上面的类,但编辑了getBytesRead()方法,因为ObjectInputStream导致了StreamCorruptedException

public long getBytesRead() {
    try {
        while (read() != -1)
            ;
    } catch (IOException e) {
        e.printStackTrace();
    }

    return bytesRead;
}

要获取内容长度,如果没有任何流,您可以使用给定的方法 getContentLength()
否则,您必须实现自己的内容长度计算。上面的方法 addPart(String name, ContentBody cb) 提供了一种方法。有关内容长度计算的更多详细信息,您可以从类 MultiPartyEntityHttpMultipart

I found my mistake. I had overwritten the method getContentLength(), which is important for the transmission, after removing my own version the file transmission works fine.

To get the size of an InputStream I used the class above but edited the method getBytesRead(), because the ObjectInputStream causes a StreamCorruptedException:

public long getBytesRead() {
    try {
        while (read() != -1)
            ;
    } catch (IOException e) {
        e.printStackTrace();
    }

    return bytesRead;
}

To get the content length, you can take the given method getContentLength() if there aren't any streams.
Otherwise you have to implement your own content length calculation. The method addPart(String name, ContentBody cb) above provides an approach. More details about the content length calculation you can get from the classes MultiPartyEntity and HttpMultipart.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文