OkHttp下载
private void testOkHttp() {
OkHttpClient okHttpClient = new OkHttpClient();
Request build = new Request.Builder()
.url(downLoadUrl)
.build();
Call newCall = okHttpClient.newCall(build);
newCall.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d(TAG, "onFailure: "+e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
long maxSize= response.body().contentLength();
mProgressbar.setMax((int) maxSize);
Log.d(TAG, "onResponse: maxSize="+maxSize);
InputStream inputStream = response.body().byteStream();
String sdPath = Environment.getExternalStorageDirectory()+ File.separator+"banmi_330.apk";
File file = new File(sdPath);
if(!file.exists()){
file.createNewFile();
}
FileOutputStream outputStream = new FileOutputStream(file);
byte [] bytes = new byte[1024];
int readLength=0;
long currLength=0;
while ((readLength=inputStream.read(bytes))!=-1) {
outputStream.write(bytes,0,readLength);
currLength+=readLength;
mProgressbar.setProgress((int) currLength);
}
outputStream.close();
inputStream.close();
}
});
}
httpurlconnection下载
private void downLoadFile() {
new Thread() {
@Override
public void run() {
super.run();
InputStream inputStream = null;
FileOutputStream outputStream = null;
try {
String filename = apkUrl.substring(apkUrl.lastIndexOf("/") + 1);
File file = new File(targetFilePath + filename);
HttpURLConnection connection = getConnection(apkUrl);
int fileLength = connection.getContentLength();
inputStream = connection.getInputStream();
outputStream = new FileOutputStream(file);
byte[] bytes = new byte[8 * 1024];
int readLength;
int currloadSize = 0;
while ((readLength = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, readLength);
currloadSize += readLength;
Log.d(TAG, "run: readlength=" + ((100 * currloadSize) / fileLength) + "%");
}
Log.d(TAG, "run: 写入文件完成");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.start();
}
public static HttpURLConnection getConnection(String fileUrl) throws IOException {
URL url = new URL(fileUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(10 * 1000);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
conn.setRequestProperty("Accept-Language", "zh-CN");
conn.setRequestProperty("Referer", fileUrl);
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Accept-Encoding", "identity");
return conn;
}