Android 下载 Zip 到 SD 卡,zip 无法读取

发布于 2024-10-11 12:04:28 字数 2276 浏览 2 评论 0原文

尝试使用以下代码将 zip 下载到 SD 卡,我得到 NullpointerException。当尝试一些询问时,我发现 zip 文件实际上没有被下载。那么,代码是否需要一些更改或 zip 有什么问题,请您帮忙吗? M只坚持了这一点。请帮助...我的代码如下...

    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String from = "http://192.168.1.63/ZipFile/Text.zip" ;
        String to = Environment.getExternalStorageDirectory() + "/newunzip/";

        try {
            ((TextView) findViewById(R.id.display)).append("\n in try after function :");
            downloadFile(from, to);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            ((TextView) findViewById(R.id.display)).append("\n \n Exception occured :");
            ((TextView) findViewById(R.id.display)).append("\n \n Exception message is :"+e.getMessage());
            ((TextView) findViewById(R.id.display)).append("\n \n Exception is :"+e.toString());
        }

    }

    private void downloadFile(String from, String to) throws Exception 
    {
        ((TextView) findViewById(R.id.display)).append("\n \n in function call :");
        HttpURLConnection conn = (HttpURLConnection)new URL(from).openConnection();
        conn.setDoInput(true);
        conn.setConnectTimeout(100000); // timeout 100 secs
        conn.connect();

        ((TextView) findViewById(R.id.display)).append("\n \n Connecting to url :"+ conn);
        InputStream input = conn.getInputStream();

        byte[] b = null;
        input.read(b);
        ((TextView) findViewById(R.id.display)).append("\n \n input method :"+ b);

        FileOutputStream fOut = new FileOutputStream(to);

        byte[] b1 = null;
        input.read(b1);
        ((TextView) findViewById(R.id.display)).append("\n \n output method :"+ b1);

        int byteCount = 0;
        byte[] buffer = new byte[4096];
        int bytesRead = -1;
        while ((bytesRead = input.read(buffer)) != -1) 
        {
            ((TextView) findViewById(R.id.display)).append("\n \n reading/writing files :");
            fOut.write(buffer, 0, bytesRead);
            byteCount += bytesRead;
        }
        fOut.flush();
        ((TextView) findViewById(R.id.display)).append("\n \n flush & close :");
        fOut.close();
    }

}

tried following code to download zip to sd card, i get NullpointerException. when tried some interrogations i come to know that the zip file is not getting downloaded actually. So would you please help whether code needs some changes or anything wrong with zip? M stuck on that point only. Please help... my code as follows...

    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String from = "http://192.168.1.63/ZipFile/Text.zip" ;
        String to = Environment.getExternalStorageDirectory() + "/newunzip/";

        try {
            ((TextView) findViewById(R.id.display)).append("\n in try after function :");
            downloadFile(from, to);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            ((TextView) findViewById(R.id.display)).append("\n \n Exception occured :");
            ((TextView) findViewById(R.id.display)).append("\n \n Exception message is :"+e.getMessage());
            ((TextView) findViewById(R.id.display)).append("\n \n Exception is :"+e.toString());
        }

    }

    private void downloadFile(String from, String to) throws Exception 
    {
        ((TextView) findViewById(R.id.display)).append("\n \n in function call :");
        HttpURLConnection conn = (HttpURLConnection)new URL(from).openConnection();
        conn.setDoInput(true);
        conn.setConnectTimeout(100000); // timeout 100 secs
        conn.connect();

        ((TextView) findViewById(R.id.display)).append("\n \n Connecting to url :"+ conn);
        InputStream input = conn.getInputStream();

        byte[] b = null;
        input.read(b);
        ((TextView) findViewById(R.id.display)).append("\n \n input method :"+ b);

        FileOutputStream fOut = new FileOutputStream(to);

        byte[] b1 = null;
        input.read(b1);
        ((TextView) findViewById(R.id.display)).append("\n \n output method :"+ b1);

        int byteCount = 0;
        byte[] buffer = new byte[4096];
        int bytesRead = -1;
        while ((bytesRead = input.read(buffer)) != -1) 
        {
            ((TextView) findViewById(R.id.display)).append("\n \n reading/writing files :");
            fOut.write(buffer, 0, bytesRead);
            byteCount += bytesRead;
        }
        fOut.flush();
        ((TextView) findViewById(R.id.display)).append("\n \n flush & close :");
        fOut.close();
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听风念你 2024-10-18 12:04:28

您正在使用空字节数据从流中读取;

    byte[] b = null;
    input.read(b);
    ((TextView) findViewById(R.id.display)).append("\n \n input method :"+ b);

评论这些行。

You are using null byte data to read from stream;

    byte[] b = null;
    input.read(b);
    ((TextView) findViewById(R.id.display)).append("\n \n input method :"+ b);

Comment these lines.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文