我正在使用OkHttp 3.1.2。我已经创建了一个文件上传,类似于原始的示例,可以在此处找到: https://github.com/square/okhttp/blob/master/samples/guide/src/main/java/okhttp3/recipes/PostMultipart.java
我找不到关于如何在用户请求时中止大文件上传的示例。我的意思不是如何获取用户请求,而是如何告诉OkHttp停止发送数据。到目前为止,我唯一能想到的解决方案是使用自定义
有其他方法吗?
RequestBody
,添加abort()
方法并重写writeTo()
方法,就像这样:public void abort() {
aborted = true;
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
Source source = null;
try {
source = Okio.source(mFile);
long transferred = 0;
long read;
while (!aborted && (read = source.read(sink.buffer(), SEGMENT_SIZE)) != -1) {
transferred += read;
sink.flush();
mListener.transferredSoFar(transferred);
}
} finally {
Util.closeQuietly(source);
}
}
有其他方法吗?