android 热更新 下载dex文件

下载dex文件出错的菜鸟们可以看看

http://blog.youkuaiyun.com/youmingyu/article/details/52583409

如果你参考这个博客上面的方法,那么恭喜你,你踩到坑了,

我就是踩到这个坑花了不少时间解决

直接上代码

public boolean downlaodFile(final String urlStr, final String path, final String fileName) {
new Thread(new Runnable() {


@Override
public void run() {
OutputStream output = null;
try {

URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();


String pathName = path + "/" + fileName;// 文件存储路径


File file = new File(pathName);
InputStream input = conn.getInputStream();
System.out.println("pathName:"+pathName);
if (file.exists()) {
System.out.println("exits");
file.delete();

}
{
File f = new File(path);
if (!f.exists()) {
f.mkdir();
}


file.createNewFile();// 新建文件
output = new FileOutputStream(file);
 byte[] buf = new byte[1024];

int ch = -1;
          
           while ((ch = input.read(buf)) != -1) {
            output.write(buf, 0, ch);
          
             
           }

output.flush();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
output.close();
downLoad=true;
System.out.println("success");
} catch (IOException e) {
System.out.println("fail");
e.printStackTrace();
}
}


}
}).start();

 long st=System.currentTimeMillis();
  while(downLoad==false&&System.currentTimeMillis()-st<3000){
 
  try {
  Thread.sleep(10);
  } catch (InterruptedException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }
 
  }
  return downLoad;
}

这个适用小文件的下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值