读取一个InputStream和使用下载管理器下载 [英] Read an inputstream and download using DownloadManager

查看:222
本文介绍了读取一个InputStream和使用下载管理器下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下载和保存文件到SD卡。文件的URL如下:

I am trying to download and save the file to sd card. The file url is as follows

http://test.com/net/Webexecute.aspx?fileId=120

此URL提供的数据流。我有以下选项来读取输入流。

This url provides a stream of data. I have the following option to read the input stream.


  • 使用通用的输入和输出流(用于连接处理器没有失败
    旁白)

  • Use generic input and output stream (no handlers for connection fail overs)

下载管理器

使用HttpURLConnection的(可能超时的机会)

Using HttpUrlConnection (possible timeout chances)

我已经做使用选项下载。但目前还没有处理程序连接失败接管。所以我决定去与选项B

I have done the download using option a. But there are no handlers for connection fail overs. So I decided to go with option b

DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Request request = new Request(Uri.parse("http://test.com/net/Webexecute.aspx?fileId="+ fileId));
request.setMimeType("application/pdf");
request.setDescription("fileDownload");
request.setTitle(fileName);
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
dm.enqueue(request);

据下载文件。但是,该文件似乎已损坏。

It is downloading the file. However, the file seems to be corrupted.

虽然做研究,我从来没有发现被下载管理器用于获取输入流并保存到文件中。有什么我缺少什么呢?

While doing the research, I never found DownloadManager being used to fetch an input stream and save that to a file. Is there anything I am lacking?

推荐答案

请改变你的code下载文件。

Please change your code to download a file.

保护无效downLoadFile(字符串fileURL)
    {

protected Void downLoadFile(String fileURL) {

    int count;
    try
    {

        URL url = new URL(fileURL);
        URLConnection conexion = url.openConnection();
        conexion.connect();
        int lenghtOfFile = conexion.getContentLength();
        InputStream is = url.openStream();

        File testDirectory = new File(Environment.getExternalStorageDirectory() + "/Download");
        if (!testDirectory.exists())
        {
            testDirectory.mkdir();
        }

        FileOutputStream fos = new FileOutputStream(testDirectory + "/filename.txt");


        byte data[] = new byte[1024];
        long total = 0;
        int progress = 0;
        while ((count = is.read(data)) != -1)
        {
            total += count;
            int progress_temp = (int) total * 100 / lenghtOfFile;

            fos.write(data, 0, count);

        }
        is.close();
        fos.close();

        readStringFromFile(testDirectory);

    }
    catch (Exception e)
    {
        Log.e("ERROR DOWNLOADING", "Unable to download" + e.getMessage());
        e.printStackTrace();
    }
    return null;

的下面方法被用来从文件中读取字符串

The Below method is used to read string from file.

public String readStringFromFile(File file){
        String response="";
        try
        {
            FileInputStream fileInputStream= new FileInputStream(file+"/filename.txt");
            StringBuilder builder = new StringBuilder();
            int ch;
            while((ch = fileInputStream.read()) != -1){
                builder.append((char)ch);
            }
            response = builder.toString();

        }
        catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return response;
    }

让我知道你仍然面临的任何问题。

Let me know of you still face any issue..

感谢

这篇关于读取一个InputStream和使用下载管理器下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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