一、url类 import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL;
public class StreamTest4 {
public static void main(String[] args) {
InputStream inStream=null;
OutputStream outStream=null;
try {
URL url=new URL("https://www.baidu.com");//windows系统,直接baidu后,直接右键-属性-复制url地址就可以
//但是mac系统,就比较无语
inStream=url.openStream();
//查看 inStream指向对象的类型是什么?
System.out.println(inStream.getClass());
outStream=new FileOutputStream("/Users/hanzhao/Desktop/1.html");
//windows系统,就随便保存为“d:\\1.png”就好,但是mac系统试了试就只能保存为.html格式
IOUtils.copy(inStream, outStream);
System.out.println("下载成功");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("地址错误"+e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("打开网页错误"+e.getMessage());
}finally {
IOUtils.closeQuietly(inStream);
IOUtils.closeQuietly(outStream);
}
}
}