- 相關(guān)推薦
如何利用java讀取網(wǎng)絡(luò)照片
Java語言作為靜態(tài)面向?qū)ο缶幊陶Z言的代表,極好地實(shí)現(xiàn)了面向?qū)ο罄碚,允許程序員以優(yōu)雅的思維方式進(jìn)行復(fù)雜的編程。以下是小編為大家搜索整理的如何利用java讀取網(wǎng)絡(luò)照片,希望能給大家?guī)韼椭?更多精彩內(nèi)容請(qǐng)及時(shí)關(guān)注我們應(yīng)屆畢業(yè)生考試網(wǎng)!
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
/*. *讀取網(wǎng)絡(luò)照片,保存到本地
* */
public class GetUrlImg {
public static void getUrlImg(String URLName,String target) throws Exception {//URLName照片地址
// target本地地址
int HttpResult = 0; //服務(wù)器返回的狀態(tài)
URL url = new URL(URLName); //創(chuàng)建URL
URLConnection urlconn = url.openConnection(); // 試圖連接并取得返回狀態(tài)碼urlconn.connect();
HttpURLConnection httpconn = (HttpURLConnection) urlconn;
HttpResult = httpconn.getResponseCode();
System.out.println(HttpResult);
if (HttpResult != HttpURLConnection.HTTP_OK) // 不等于HTTP_OK說明連接不成功System.out.print("fail");
else {
int filesize = urlconn.getContentLength(); // 取數(shù)據(jù)長(zhǎng)度System.out.println(filesize);
BufferedInputStream bis=new BufferedInputStream(urlconn.getInputStream());
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(target));
byte[] buffer = new byte[1024]; //創(chuàng)建存放輸入流的緩沖
int num = -1; //讀入的字節(jié)數(shù)
while (true) {
num = bis.read(buffer); // 讀入到緩沖區(qū)
if (num ==-1){
bos.flush();
break; //已經(jīng)讀完
}
bos.flush();
bos.write(buffer,0,num);
}
bos.close();
bis.close();
}
}
public static void main(String[] args) throws Exception{
GetUrlImg.getUrlImg("java中equals和==的區(qū)別
【如何利用java讀取網(wǎng)絡(luò)照片】相關(guān)文章:
Java如何讀取文本文件07-06
從Java的jar文件中如何讀取數(shù)據(jù)的方法10-18
java讀取解析xml文件實(shí)例08-05
java如何構(gòu)造09-21
PHP如何使用DOM和simplexml讀取xml文檔07-22
如何創(chuàng)建java包06-11
Java的就業(yè)前景如何07-28
新手如何學(xué)習(xí)Java07-06