0

这是下载 3pg 视频文件的代码。也许有人可以让我知道它有什么问题。我真的需要帮助。无法下载文件:

块引用

        protected String doInBackground(String... sUrl) {
            byte[] data = new byte[1024];
            int count = 0;
            FileOutputStream fos = null;
            BufferedOutputStream output = null;
            try {
            URL url= new URL(sUrl[0]);
            URLConnection connection = url.openConnection();
            connection.connect();
            Log.v("URL"," " + connection.getURL());
            Log.v("File ", "length = " + connection.getContentLength());
            // download the file
            BufferedInputStream input = new BufferedInputStream(connection.getInputStream());
            File f = new File(Environment.getExternalStorageDirectory().getPath() + "/twokids.3gp");
            f.setWritable(true,true);
            try {
                fos = new FileOutputStream(f);
                } catch (FileNotFoundException fnfe) {
                    fnfe.getStackTrace();
                } catch (SecurityException se) {
                    se.getStackTrace();
                }

                output = new BufferedOutputStream(fos);                                                                                                                        

            while ((count = input.read(data)) != -1) {
                Log.v("Count="," " + count);
                output.write(data, 0, count);
            }
            output.flush();
            output.close();
            input.close();
            Log.v("Download","Finished");
            } catch (IndexOutOfBoundsException iobe) {
            iobe.getStackTrace();
            } catch (IOException ioe) {
            ioe.getStackTrace();
            } catch (NullPointerException npe) {
            npe.getStackTrace();
            }
            catch (Exception e) {
            e.getStackTrace();
            }

        return null;
    }

奇怪的是,有时方法 connection.getContentLength() 返回 -1 而不是文件长度。

4

0 回答 0