编程在Android上下载一个文件 [英] Download a file programatically on Android

查看:178
本文介绍了编程在Android上下载一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从Web服务器下载文件编程。下载完成后,我检查了锉的大小,扩展和其他所有参数都是正确的,但我当我尝试播放的媒体播放器则表明它已损坏的文件。

plz帮助..

code:

 字节[] B = NULL;
    在的InputStream = NULL;
    B =新的字节[的Integer.parseInt(尺寸)]。 //该文件的大小。
    在= OpenHttpConnection(URL);
    in.read(B);
    附寄();

    文件夹=新的文件(/ SD卡,文件夹);
   布尔查= folder.mkdirs();

   Log.d(HttpDownload,检查+查);

   myfile文件=新的文件(/ SD卡/文件夹/+姓名);


    myFile.createNewFile();
   的OutputStream filoutputStream =新的FileOutputStream(MYFILE);

   filoutputStream.write(B);

   filoutputStream.flush();

   filoutputStream.close();
 

解决方案

这是一些工作code我有下载指定的URL给定的文件对象。 File对象(OUTPUTFILE)刚刚用新的文件(路径)创建的,我没有叫createNewFile或任何东西。

 私有静态无效downloadFile(字符串URL,文件OUTPUTFILE){
  尝试 {
      网址U =新的网址(URL);
      URLConnection的康恩= u.openConnection();
      INT CONTENTLENGTH = conn.getContentLength();

      的DataInputStream流=新的DataInputStream(u.openStream());

        byte []的缓冲区=新的字节[CONTENTLENGTH]
        stream.readFully(缓冲液);
        stream.close();

        DataOutputStream类FOS =新DataOutputStream类(新的FileOutputStream(OUTPUTFILE));
        fos.write(缓冲液);
        fos.flush();
        fos.close();
  }赶上(FileNotFoundException异常E){
      返回; //吞下404
  }赶上(IOException异常E){
      返回; //吞下404
  }
}
 

i am downloading files from web server programatically. after download is complete, i checked the file.the size ,extension and all other parameters are correct but i when i try to play that file in media player it is showing that it is corrupt.

plz help..

code:

    byte[] b = null;
    InputStream in = null;
    b = new byte[Integer.parseInt(size)];    // size of the file.
    in = OpenHttpConnection(URL);            
    in.read(b);
    in.close();

    File folder = new File("/sdcard", "folder");
   boolean check = folder.mkdirs();

   Log.d("HttpDownload", "check " + check);

   File myFile = new File("/sdcard/folder/" + name);


    myFile.createNewFile();
   OutputStream filoutputStream = new FileOutputStream(myFile);

   filoutputStream.write(b);

   filoutputStream.flush();

   filoutputStream.close();

解决方案

This is some working code I have for downloading a given URL to a given File object. The File object (outputFile) has just been created using new File(path), I haven't called createNewFile or anything.

private static void downloadFile(String url, File outputFile) {
  try {
      URL u = new URL(url);
      URLConnection conn = u.openConnection();
      int contentLength = conn.getContentLength();

      DataInputStream stream = new DataInputStream(u.openStream());

        byte[] buffer = new byte[contentLength];
        stream.readFully(buffer);
        stream.close();

        DataOutputStream fos = new DataOutputStream(new FileOutputStream(outputFile));
        fos.write(buffer);
        fos.flush();
        fos.close();
  } catch(FileNotFoundException e) {
      return; // swallow a 404
  } catch (IOException e) {
      return; // swallow a 404
  }
}

这篇关于编程在Android上下载一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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