Android的下载多个文件的InputStream的FileOutputStream [英] android downloading multiple files with InputStream FileOutputStream

查看:301
本文介绍了Android的下载多个文件的InputStream的FileOutputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有下载某些文件,献给我的是谁主办的远程位置他文件的客户端应用程序,而我这样做使用下面的code:

So I have an app which downloads certain files, dedicated to a client of mine who is hosting his files on a remote location, and i'm doing so using the code below:

public class DownloadService extends IntentService {

    private int result = Activity.RESULT_CANCELED;
    public DownloadService() {
        super("DownloadService");
    }
    @Override
    protected void onHandleIntent(Intent intent) {
        String urlPath = intent.getStringExtra(URL);
        String fileName = intent.getStringExtra(FILENAME);
        File output = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
                fileName);
        if (output.exists()) {
            output.delete();
        }

        URLConnection streamConnection = null;
        InputStream stream = null;
        FileOutputStream fos = null;
        try {

            URL url = new URL(urlPath);
            streamConnection = url.openConnection();
            stream = streamConnection.getInputStream();
            streamConnection.connect();
            long lengthofFile = streamConnection.getContentLength();
            InputStream reader = stream;
            bis = new BufferedInputStream(reader);
            fos = new FileOutputStream(output.getPath());
            int next = -1;
            int progress = 0;
            int bytesRead = 0;
            byte buffer[] = new byte[1024];
            while ((bytesRead = bis.read(buffer)) > 0) {
                fos.write(buffer, 0, bytesRead);
                progress += bytesRead;
                int progressUpdate = (int)((progress * 100) / lengthofFile);
                Intent testIntent = new Intent(".MESSAGE_INTENT");
                testIntent.putExtra(PERCENTAGE, progressUpdate);
                sendBroadcast(testIntent);
            }
            result = Activity.RESULT_OK;
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        publishResults(output.getAbsolutePath(), result);
    }

    private void publishResults(String outputPath, int result) {
        Intent intent = new Intent(".MESSAGE_INTENT");
        intent.putExtra(FILEPATH, outputPath);
        intent.putExtra(RESULT, result);
        sendBroadcast(intent);
    }

}

和调用这个服务我会用:

and to call this service i would use:

Intent intent = new Intent(MainActivity.getAppContext(), DownloadService.class);
intent.putExtra(DownloadService.FILENAME, downloadFileName[item]);
intent.putExtra(DownloadService.URL, urlDownload[item]);
MainActivity.getAppContext().startService(intent);

现在这允许用户下载一个文件的时间,但是如果用户下载其他文件,第二个文件将不得不等待,直到第一个文件下载完毕。
现在是什么在我的情况正在发生的事情是:
1 - 首先下载FILE_1是下载,并且在状态说FILE_1。
2-用户点击新文件下载时,状态改变第一文件名的第二文件名,并等待直到FILE_1完成下载开始与FILE_2然而活性下载从FILE_1变为FILE_2

now this allows user to download one file at a time, however if the user downloads another file, the second file will have to wait till the first file is done downloading. now what is happening in my case is: 1- First download FILE_1 is downloading, and in the status is says FILE_1. 2- User clicks a new file download, the status changes the first file name to the second file name, and waits till FILE_1 finishes download to start with FILE_2 however the active download is changed from FILE_1 to FILE_2.

问题:
有没有办法来调用下载服务多次为多个文件?
是否有可能解决我面临的问题?治疗意图下载服务为两个不同的意图?

questions: is there a way to call DownloadService multiple times for multiple files? is it possible to fix the problem i'm facing? treating download intent services as two different intents?

更新
我设法通过分配每个文件的唯一诠释ID来解决这个问题,每个ID将指向ListView中的位置而显示正在下载或排队的文件,然后我用它自己的每一个文件的工作。

UPDATE I managed to solve this issue by assigning a unique Int ID per file, each ID will point to a position in the listview which displays the files being downloaded or queued, then i work with each file on it's own.

推荐答案

继code使用的commons-IO-2.4.jar 库,让您的工作更轻松通过处理水平低的数据移动,你会专注于法在手

Following code uses commons-io-2.4.jar library to make your work easy by handling low level data movements as you would focus on method in hand

URL someUrl = new URL("your url String"); //
File someFile = new File("your file, where you want to save the data");
FileUtils.copyURLToFile(someUrl, someFile );

如果你想调用这个说法很少的时间来从服务器下载以下code可能给你一个想法,你可能想要做什么不同的文件,但是你必须把它写这相当于code运行在android系统要大概的AsyncTask

if you want to call this statement few time to download different files from the server following code might give you an idea what you might want to do, but you will have to write it's equivalent code to run in android which you want to probably AsyncTask

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.io.FileUtils;

public class DownloadTest {

    public static void main(String[] args) {

        Thread thread = new Thread(){
            @Override
            public void run() {
                // TODO Auto-generated method stub
                super.run();
                try {
                    dowanloadFile(new URL("some url"), new File("some file"));
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        thread.start();


    }

    private static void dowanloadFile(URL url, File file){
        try {
            FileUtils.copyURLToFile(url, file );
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

这篇关于Android的下载多个文件的InputStream的FileOutputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆