通过asyncTask从不同的URL下载不同文件夹中的文件,可以按照以下步骤进行:
以下是一个示例代码:
public class DownloadTask extends AsyncTask<URL, Integer, Boolean> {
private Context context;
private String folderPath;
public DownloadTask(Context context, String folderPath) {
this.context = context;
this.folderPath = folderPath;
}
@Override
protected Boolean doInBackground(URL... urls) {
for (int i = 0; i < urls.length; i++) {
try {
URL url = urls[i];
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
int fileLength = connection.getContentLength();
File folder = new File(folderPath);
if (!folder.exists()) {
folder.mkdirs();
}
File file = new File(folder, "file" + i + ".txt");
FileOutputStream outputStream = new FileOutputStream(file);
InputStream inputStream = connection.getInputStream();
byte[] buffer = new byte[1024];
int len;
int total = 0;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
total += len;
publishProgress((int) ((total * 100) / fileLength));
}
outputStream.close();
inputStream.close();
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
return true;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
// 更新下载进度
int progress = values[0];
// TODO: 更新UI界面的进度条
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if (result) {
// 下载成功
Toast.makeText(context, "下载完成", Toast.LENGTH_SHORT).show();
} else {
// 下载失败
Toast.makeText(context, "下载失败", Toast.LENGTH_SHORT).show();
}
}
}
使用示例:
URL url1 = new URL("http://example.com/file1.txt");
URL url2 = new URL("http://example.com/file2.txt");
DownloadTask downloadTask = new DownloadTask(context, folderPath);
downloadTask.execute(url1, url2);
请注意,以上示例代码仅为演示如何通过AsyncTask从不同的URL下载不同文件夹中的文件,并未涉及到具体的腾讯云产品。根据实际需求,你可以结合腾讯云的相关产品来实现更完善的解决方案。
领取专属 10元无门槛券
手把手带您无忧上云