package NetWork;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.hamcrest.core.Is;
public class URLtest1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
HttpURLConnection openConnection = null;
InputStream is = null;
FileOutputStream fos = null;
try {
URL url = new URL("https://www.toopic.cn/public/uploads/image/20200404/20200404182849_78999.jpg");
openConnection = (HttpURLConnection)url.openConnection();
openConnection.connect();
is = openConnection.getInputStream();
fos = new FileOutputStream("beauty4.jpg");
byte[] buffer = new byte[5];
int len;
while((len = is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(is!=null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(openConnection != null){
try {
openConnection.disconnect();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
4.2 URL
最新推荐文章于 2024-08-29 18:44:48 发布