如何将完整数据下载到字节数组

时间:2012-02-21 04:54:21

标签: android download

我想通过网址下载zip文件。我想加密,而无需写入SD卡。如何从URL获取完整的字节数据? 提前致谢

protected String doInBackground(String... url) {
        int count;
        try {
            URL url1 = new URL(url[0]);
            URLConnection conexion = url1.openConnection();
            conexion.connect();
            // this will be useful so that you can show a tipical 0-100%
            // progress bar
            int lenghtOfFile = conexion.getContentLength();

            // download the file
            InputStream input = new BufferedInputStream(url1.openStream());
            OutputStream output = new FileOutputStream(
                    "/sdcard/downloaded.zip");

            byte data[] = new byte[1024];
            long total = 0;

            while ((count = input.read(data)) != -1) {

                total += count;
                // publishing the progress....
                publishProgress((int) (total * 100 / lenghtOfFile));
                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();
        } catch (Exception e) {
        }
        return null;
    }

1 个答案:

答案 0 :(得分:1)

在下载呼叫ByteArrayOutputStream()功能后使用toByteArray()