gpt4 book ai didi

android - 如何知道在android中上传到服务器的文件百分比

转载 作者:行者123 更新时间:2023-11-29 17:17:03 25 4
gpt4 key购买 nike

我正在使用此代码将文件从我的 android 上传到服务器,但我不知道上传了多少文件(例如 30MB 来自 100MB)!有什么办法吗?

String url = "http://yourserver";
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),
"yourfile");
try {
HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost(url);

InputStreamEntity reqEntity = new InputStreamEntity(
new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
//Do something with response...

} catch (Exception e) {
// show error
}

最佳答案

为此,您使用了 AsyncTask 并使用 publishProgress() 方法设置了 ProgressDialog 百分比。有关更多详细信息,请访问此 link .

关于android - 如何知道在android中上传到服务器的文件百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38420417/

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