这对我们有用,使用一些 Apache 库
FileOutputStream fos = new FileOutputStream("Your File");
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
HttpClient client = new DefaultHttpClient(ccm, params);
HttpGet httpGet = new HttpGet(downloadURL);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
Log.d(LOG_TAG, "code is " + statusCode);
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
byte[] buffer = new byte[1024];
int len1 = 0;
while ( (len1 = content.read(buffer)) > 0 ) {
fos.write(buffer,0, len1);
}
success = true;
} else {
Log.e(LOG_TAG_JSON, "Failed to download file " + downloadURL);
}
if (null != response.getEntity())
{
response.getEntity().consumeContent();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
Log.e(LOG_TAG, "downloadVersion " + e.toString());
e.printStackTrace();
}