代码实例
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class URLTest {
public static void main(String[] args) throws URISyntaxException {
InputStream inputStream=null;
try {
//创建URL对象,例如:百度搜索中国好声音
//wd关键词的值即为"中国好声音"的UTF-8编码,可以使用URLEncoder对字符进行编码
String str="http://www.baidu.com/s?wd=%E4%B8%AD%E5%9B%BD%E5%A5%BD%E5%A3%B0%E9%9F%B3"
+ "&rsv_spt=1&issp=1&f=8&rsv_bp=0&rsv_idx=2&ie=utf-8&tn=baiduhome_pg"
+ "&rsv_enter=1&inputT=1498";
URL url = new URL(str);
//创建URLConnection对象
URLConnection openConnection = url.openConnection();
//编码类型
String encoding=null;
//下面打印响应头部分
System.out.println("-----------响应头开始-----------");
Map<String, List<String>> headerFields = openConnection.getHeaderFields();
Set<Entry<String, List<String>>> entrySet = headerFields.entrySet();
Iterator<Entry<String, List<String>>> iterator = entrySet.iterator();
while(iterator.hasNext()) {
Entry<String, List<String>> next = iterator.next();
String key = next.getKey();
List<String> value = next.getValue();
String string = value.toString();
System.out.println(key+":"+string);
if("Content-Type".equals(key)) {
int indexOf = string.indexOf("charset=");
encoding = string.substring(indexOf+8,string.length()-1);
}
}
System.out.println("-----------响应头结束-----------");
//获取URLConnection对象的输入流
inputStream=openConnection.getInputStream();
//通过IO来读取流,写入文件
int c;
int count=0;
File file=new File("baiduOSC.html");
FileOutputStream fileOutputStream = new FileOutputStream(file);
BufferedOutputStream outputStream = new BufferedOutputStream(fileOutputStream);
inputStream=new BufferedInputStream(inputStream);
Reader inputStreamReader = new InputStreamReader(inputStream,encoding);
while((c=inputStreamReader.read())!=-1) {
outputStream.write(c);
count++;
}
inputStreamReader.close();
outputStream.close();
System.out.println("文件"+file.getName()+"的大小为"+Math.ceil(count/1024.0)+"kb");
System.out.println("下载结束");
} catch (IOException e) {
System.err.println("无法下载");
} finally {
if(inputStream!=null) {
try {
inputStream.close();
} catch(Exception ex) {
//不处理
}
}
}
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class URLTest {
public static void main(String[] args) throws URISyntaxException {
InputStream inputStream=null;
try {
//创建URL对象,例如:百度搜索中国好声音
//wd关键词的值即为"中国好声音"的UTF-8编码,可以使用URLEncoder对字符进行编码
String str="http://www.baidu.com/s?wd=%E4%B8%AD%E5%9B%BD%E5%A5%BD%E5%A3%B0%E9%9F%B3"
+ "&rsv_spt=1&issp=1&f=8&rsv_bp=0&rsv_idx=2&ie=utf-8&tn=baiduhome_pg"
+ "&rsv_enter=1&inputT=1498";
URL url = new URL(str);
//创建URLConnection对象
URLConnection openConnection = url.openConnection();
//编码类型
String encoding=null;
//下面打印响应头部分
System.out.println("-----------响应头开始-----------");
Map<String, List<String>> headerFields = openConnection.getHeaderFields();
Set<Entry<String, List<String>>> entrySet = headerFields.entrySet();
Iterator<Entry<String, List<String>>> iterator = entrySet.iterator();
while(iterator.hasNext()) {
Entry<String, List<String>> next = iterator.next();
String key = next.getKey();
List<String> value = next.getValue();
String string = value.toString();
System.out.println(key+":"+string);
if("Content-Type".equals(key)) {
int indexOf = string.indexOf("charset=");
encoding = string.substring(indexOf+8,string.length()-1);
}
}
System.out.println("-----------响应头结束-----------");
//获取URLConnection对象的输入流
inputStream=openConnection.getInputStream();
//通过IO来读取流,写入文件
int c;
int count=0;
File file=new File("baiduOSC.html");
FileOutputStream fileOutputStream = new FileOutputStream(file);
BufferedOutputStream outputStream = new BufferedOutputStream(fileOutputStream);
inputStream=new BufferedInputStream(inputStream);
Reader inputStreamReader = new InputStreamReader(inputStream,encoding);
while((c=inputStreamReader.read())!=-1) {
outputStream.write(c);
count++;
}
inputStreamReader.close();
outputStream.close();
System.out.println("文件"+file.getName()+"的大小为"+Math.ceil(count/1024.0)+"kb");
System.out.println("下载结束");
} catch (IOException e) {
System.err.println("无法下载");
} finally {
if(inputStream!=null) {
try {
inputStream.close();
} catch(Exception ex) {
//不处理
}
}
}
}
}
运行实例
-----------响应头开始-----------
BDPAGETYPE:[3]
Transfer-Encoding:[chunked]
null:[HTTP/1.1 200 OK]
Server:[BWS/1.1]
Cxy_ex:[1438852608+2445156729+d41d8cd98f00b204e9800998ecf8427e]
BDQID:[0xc8cf935400001d62]
Connection:[Keep-Alive]
P3P:[CP=" OTI DSP COR IVA OUR IND COM ", CP=" OTI DSP COR IVA OUR IND COM "]
Date:[Thu, 06 Aug 2015 09:16:48 GMT]
X-UA-Compatible:[IE=Edge,chrome=1]
Cache-Control:[private]
Cxy_all:[baiduhome_pg+66d9c0f29a9055c1f190fe638dacaa45]
Set-Cookie:[H_PS_PSSID=11193_16469_1438_12867_14668_16520_16513_16662_16424_16515_15459_12141_13932_16721; path=/; domain=.baidu.com, BDSVRTM=187; path=/, BD_CK_SAM=1;path=/, BDRCVFR[feWj1Vr5u3D]=mk3SLVN4HKm; path=/; domain=.baidu.com, PSTM=1438852608; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com, BIDUPSID=5DAE500615A53E18FDB62639C686768B; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com, BAIDUID=5DAE500615A53E18FDB62639C686768B:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com]
Vary:[Accept-Encoding]
BDUSERID:[0]
X-Powered-By:[HPHP]
Content-Type:[text/html;charset=utf-8]
-----------响应头结束-----------
文件baiduOSC.html的大小为491.0kb
下载结束
-----------响应头开始-----------
BDPAGETYPE:[3]
Transfer-Encoding:[chunked]
null:[HTTP/1.1 200 OK]
Server:[BWS/1.1]
Cxy_ex:[1438852608+2445156729+d41d8cd98f00b204e9800998ecf8427e]
BDQID:[0xc8cf935400001d62]
Connection:[Keep-Alive]
P3P:[CP=" OTI DSP COR IVA OUR IND COM ", CP=" OTI DSP COR IVA OUR IND COM "]
Date:[Thu, 06 Aug 2015 09:16:48 GMT]
X-UA-Compatible:[IE=Edge,chrome=1]
Cache-Control:[private]
Cxy_all:[baiduhome_pg+66d9c0f29a9055c1f190fe638dacaa45]
Set-Cookie:[H_PS_PSSID=11193_16469_1438_12867_14668_16520_16513_16662_16424_16515_15459_12141_13932_16721; path=/; domain=.baidu.com, BDSVRTM=187; path=/, BD_CK_SAM=1;path=/, BDRCVFR[feWj1Vr5u3D]=mk3SLVN4HKm; path=/; domain=.baidu.com, PSTM=1438852608; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com, BIDUPSID=5DAE500615A53E18FDB62639C686768B; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com, BAIDUID=5DAE500615A53E18FDB62639C686768B:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com]
Vary:[Accept-Encoding]
BDUSERID:[0]
X-Powered-By:[HPHP]
Content-Type:[text/html;charset=utf-8]
-----------响应头结束-----------
文件baiduOSC.html的大小为491.0kb
下载结束