天天看点

android download file and save to sdCard

方法一:   

public void DownloadFromUrl(String DownloadUrl, String fileName) {

           try {

                   File root = android.os.Environment.getExternalStorageDirectory();               

                   File dir = new File (root.getAbsolutePath() + "/xmls");

                   if(dir.exists()==false) {

                        dir.mkdirs();

                   }

                   URL url = new URL(DownloadUrl); //you can write here any link

                   File file = new File(dir, fileName);

                   long startTime = System.currentTimeMillis();

                   Log.d("DownloadManager", "download begining");

                   Log.d("DownloadManager", "download url:" + url);

                   Log.d("DownloadManager", "downloaded file name:" + fileName);

                   URLConnection ucon = url.openConnection();

                   InputStream is = ucon.getInputStream();

                   BufferedInputStream bis = new BufferedInputStream(is);

                   ByteArrayBuffer baf = new ByteArrayBuffer(5000);

                   int current = 0;

                   while ((current = bis.read()) != -1) {

                      baf.append((byte) current);

                   }

                   FileOutputStream fos = new FileOutputStream(file);

                   fos.write(baf.toByteArray());

                   fos.flush();

                   fos.close();

                   Log.d("DownloadManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");

           } catch (IOException e) {

               e.printStackTrace();

               Log.d("DownloadManager", "Error: " + e);

           }

        }

方法二:

    protected void download() {

        File root = Environment.getExternalStorageDirectory();

        File file = new File(root, "myPDF" + ".pdf");

        String content = null;

        try {

            if (root.canWrite()) {

                String[] paramterNames = new String[1];

                String[] paramterValues = new String[1];

                paramterNames[0] = "respFormat";

                paramterValues[0] = "pdf";

                URL url = new URL(uri);

                try {

                    // Read the PDF from the URL and save to

                    // a local file

                    BufferedInputStream bis = new BufferedInputStream(

                            url.openStream());

                    BufferedOutputStream bos = new BufferedOutputStream(

                            new FileOutputStream(file));

                    byte[] buff = new byte[2048];

                    int bytesRead;

                    // Simple read/write loop.

                    while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {

                        bos.write(buff, 0, bytesRead);

                    }

                    bos.flush();

                    bos.close();

                    bis.close();

                } catch (NullPointerException npe) {

                    System.out.println("FAILED.\n[" + npe.getMessage() + "]\n");

                }

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

    }