java

位置:IT落伍者 >> java >> 浏览文章

java 读取网络照片


发布日期:2022年03月13日
 
java 读取网络照片

import javaioBufferedInputStream;

import javaioBufferedOutputStream;

import javaioFileOutputStream;

import HttpURLConnection;

import URL;

import URLConnection;

/* *读取网络照片保存到本地

* */

public class GetUrlImg {

public static void getUrlImg(String URLNameString target) throws Exception {//URLName照片地址

// target本地地址

int HttpResult = ; //服务器返回的状态

URL url = new URL(URLName); //创建URL

URLConnection urlconn = urlopenConnection(); // 试图连接并取得返回状态码nnect();

HttpURLConnection httpconn = (HttpURLConnection) urlconn;

HttpResult = ();

Systemoutprintln(HttpResult);

if (HttpResult != HttpURLConnectionHTTP_OK) // 不等于HTTP_OK说明连接不成功Systemoutprint(fail);

else {

int filesize = urlconngetContentLength(); // 取数据长度Systemoutprintln(filesize);

BufferedInputStream bis=new BufferedInputStream(urlconngetInputStream());

BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(target));

byte[] buffer = new byte[]; //创建存放输入流的缓沖

int num = ; //读入的字节数

while (true) {

num = bisread(buffer); // 读入到缓沖区

if (num ==){

bosflush();

break; //已经读完

}

bosflush();

boswrite(buffernum);

}

bosclose();

bisclose();

}

}

public static void main(String[] args) throws Exception{

GetUrlImggetUrlImg();

}

}

               

上一篇:在 java 里面调用 CUI 程序的方法

下一篇:八个改善Java遗留系统的技巧