2014年1月8日 星期三

httpClient下載(同樣適用 android)

httpClient下載(同樣適用 android)



 


因為工作需要,所以找到這一篇,歡迎有需要的同好,一起來C/P一下。


 


資料來源:http://wenku.baidu.com/view/0fd0280a03d8ce2f00662364.html


 









//httpClient下載(同樣適用 android)
//android httpclient httpget Download

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpClientTest
{
private final static String REMOTE_FILE_URL="http://localhost:8080/test/apache-tomcat-7.0.30.zip";
private final static int BUFFER = 1024;
public static void main(String[] args)
{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(REMOTE_FILE_URL);

try
{
HttpResponse response = httpClient.execute(httpGet);
InputStream in = response.getEntity().getContent();
FileOutputStream out = new FileOutputStream(new File("E:\\tomcat.zip"));
byte[] b = new byte[BUFFER];
int len = 0;
while((len=in.read(b))!= -1)
{
out.write(b,0,len);
}
in.close();
out.close();
}
catch(ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
//httpGet.releaseConnection();
httpClient.getConnectionManager().shutdown();
}

System.out.println("download, success!!");
}
}


 


沒有留言:

張貼留言