//1、使用URLConnection去下载网上任意一个资源
import javax.swing.*;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class URLConnection {
public static void main(String[] args) {
URL url = null;
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
BufferedInputStream bis = null;
FileOutputStream outputStream =null;
BufferedOutputStream bos = null;
try {
url = new URL("https://baike.baidu.com/pic/ASCII/309296/1/e850352ac65c103880a07b53bc119313b17e8941?fr=lemma&ct=single#aid=1&pic=e850352ac65c103880a07b53bc119313b17e8941");
urlConnection = (HttpURLConnection) url.openConnection();
inputStream = urlConnection.getInputStream();
bis = new BufferedInputStream(inputStream, 2048);
outputStream = new FileOutputStream("ASCII");
bos = new BufferedOutputStream(outputStream);
int read;
while ((read= bis.read())!=-1){
bos.write(read);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(bis!=null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bos!=null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
4.6作业
最新推荐文章于 2024-08-31 10:44:02 发布