android下文件下载 -电脑资料

电脑资料 时间:2019-01-01 我要投稿
【www.unjs.com - 电脑资料】

   

   

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    public static void downFile(final String url){

    new Thread(){

    public void run(){

    FileOutputStream s=null;

    try{

    InputStream input=null;

    URLConnection httpUrlConnection= new URL(url).openConnection();

    //int contentLength = httpUrlConnection.getContentLength();

    input =httpUrlConnection.getInputStream();

    File file = new File("C:\\Users\\Administrator\\Desktop\\Apknew.apk");

    //如果目标文件已经存在,则删除,

android下文件下载

电脑资料

android下文件下载》(https://www.unjs.com)。产生覆盖旧文件的效果

    if(file.exists())

    {

    file.delete();

    }

    s = new FileOutputStream(file);

    byte[] buffer = new byte[4*1024];

    // 读取到的数据长度

    int len;

    while((len=input.read(buffer)) != -1){

    os.write(buffer,0,len); //这里不能写成os.write(buffer)

    }

    os.flush();

    os.close();

    input.close();//这里一定不能忘记关闭输入流

    //Log.v("cmd", "文件下载完毕,路径为:"+file.getAbsolutePath());

    //update();

    System.out.println("下载完毕");

    } catch (Exception e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }finally{

    }

    }

    }.start();

    }

最新文章