gpt4 book ai didi

java - 如何使用 httpClient 4 获取上传进度?

转载 作者:行者123 更新时间:2023-12-01 04:57:35 25 4
gpt4 key购买 nike

这里是下载函数,我可以使用监听器来获取下载进度,请问如何实现像下载功能一样获取上传进度。谢谢!

public void doGetToFile(String url, String localFilePath,CloudStatusListener listener) throws RestHttpException, HttpException {
final HttpGet request = new HttpGet(url);
final HttpResponse resp;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();

BasicCredentialsProvider creds= new BasicCredentialsProvider();
creds.setCredentials(new AuthScope(CloudClient.CLOUDHOST,CloudClient.CLOUDPORT),new UsernamePasswordCredentials(UserName,Password));
httpClient.setCredentialsProvider(creds);


resp = httpClient.execute(request);
long totalnum=resp.getEntity().getContentLength();
if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
FileOutputStream out = new FileOutputStream(localFilePath);

InputStream inputStream=resp.getEntity().getContent();

long bytesRead=0;
int bufferSize=listener.progressInterval();
byte b[]=new byte[bufferSize];

int cnt=0;
while((cnt=inputStream.read(b))!=-1)
{
out.write(b,0,cnt);
bytesRead+=cnt;
listener.onProgress(bytesRead, totalnum);
}
out.flush();
out.close();

//resp.getEntity().writeTo(out);

out.close();
return;
} else {
final String response = EntityUtils.toString(resp.getEntity());
throw new RestHttpException(resp.getStatusLine().getStatusCode(), response);
}
} catch (final IOException e) {
e.printStackTrace();
throw new HttpException("IOException " + e.toString());
}
}

最佳答案

我已经解决了这个问题,你可以这样做:

class FileBodyCounter extends FileBody {
private volatile long byteCount;
private volatile CloudStatusListener listener;
private volatile long totalnum;

public FileBodyCounter(File file,CloudStatusListener listener,long totalnum) {
super(file);
this.listener=listener;
this.totalnum=totalnum;
}

public long getBytesWritten() {
return byteCount;
}

@Override
public void writeTo(OutputStream out) throws IOException {
super.writeTo(new FilterOutputStream(out) {
//
// Other write() methods omitted for brevity.
// Implement for better performance
public void write(int b) throws IOException {
byteCount++;
super.write(b);
if (byteCount % listener.progressInterval() == 0 ||
byteCount == totalnum){
listener.onProgress(byteCount,totalnum);
}
}
});
}

}

post函数是这样的:

public String doPostMultipart(String url, 
String filePath,
List<NameValuePair> params,
CloudStatusListener listener
)
throws RestHttpException,
HttpException,
IOException {

if (params == null) params = EMPTY_PARAMS;

HttpPost request = new HttpPost(url);


MultipartEntity reqEntity = new MultipartEntity();
if (filePath != null && !filePath.isEmpty()) {
FileBody bin = new FileBodyCounter(
new File(filePath),
listener,
new FileInputStream(
new File(filePath)
).available());
reqEntity.addPart("fileUpload", bin);
}

for (NameValuePair kv : params) {
multipartAddKV(reqEntity, kv.getName(), kv.getValue());
}

request.setEntity(reqEntity);

return execute(request);
}

关于java - 如何使用 httpClient 4 获取上传进度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13860200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com