下载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;
}
这个适用小文件的下载