您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
javase实现网络url文件下载
发布时间:2017-09-22 17:31:42编辑:雪饮阅读()
所需附件打包下载:
package test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
public class HttpClientTest {
private final static String REMOTE_FILE_URL = "http://www.gaojiupan.cn/d/file/mobanfenxiang/zuopinzhanshi/gerenwangzhan/92d3a3bb18517b3f527c3bcab153d02b.jpg";
private final static int BUFFER = 1024;
public static void main(String[] args) {
HttpClient client = new HttpClient();
GetMethod httpGet = new GetMethod(REMOTE_FILE_URL);
try {
client.executeMethod(httpGet);
InputStream in = httpGet.getResponseBodyAsStream();
FileOutputStream out = new FileOutputStream(new File("E:\\test_jar\\www_gaojiupan_cn.jpg"));
byte[] b = new byte[BUFFER];
int len = 0;
while((len=in.read(b))!= -1){
out.write(b,0,len);
}
in.close();
out.close();
}catch (HttpException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
httpGet.releaseConnection();
}
System.out.println("download, success!!");
}
}
关键字词:javase,网络,url,文件下载