如何从单个超链接下载多个文件

时间:2014-03-05 15:41:52

标签: java android http

我正在处理一项任务,我需要从单个超链接下载多个文件。当我调用一个API链接时,我希望它返回多个文件。

我目前的代码只下载一个文件:

try {
    URL url = new URL(f_url[0]);
    URLConnection conection = url.openConnection();
    conection.connect();
    // getting file length
    int lenghtOfFile = conection.getContentLength();

    // input stream to read file - with 8k buffer
    InputStream input = new BufferedInputStream(url.openStream(), 8192);

    // Output stream to write file
    OutputStream output = new FileOutputStream("/sdcard/downloadedfile.jpg");

    byte data[] = new byte[1024];

    long total = 0;

    while ((count = input.read(data)) != -1) {
        total += count;
        // publishing the progress....
        // After this onProgressUpdate will be called
        publishProgress(""+(int)((total*100)/lenghtOfFile));

        // writing data to file
        output.write(data, 0, count);
    }

    // flushing output
    output.flush();

    // closing streams
    output.close();
    input.close();

} catch (Exception e) {
    Log.e("Error: ", e.getMessage());
}

1 个答案:

答案 0 :(得分:0)

检查此site。它会下载多个文件并在屏幕上显示其内容