如何利用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 {/pic/p>
/pic/p>
int HttpResult = 0; /pic/p>
URL url = new URL(URLName); /pic/p>
URLConnection urlconn = url.openConnection(); /pic/p>
HttpURLConnection httpconn = (HttpURLConnection) urlconn;
HttpResult = httpconn.getResponseCode();
System.out.println(HttpResult);
if (HttpResult != HttpURLConnection.HTTP_OK) /pic/p>
else {
int filesize = urlconn.getContentLength(); /pic/p>
BufferedInputStream bis=new BufferedInputStream(urlconn.getInputStream());
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(target));
byte[] buffer = new byte[1024]; /pic/p>
int num = -1; /pic/p>
while (true) {
num = bis.read(buffer); /pic/p>
if (num ==-1){
bos.flush();
break; /pic/p>
}
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如何讀取CSV07-08
java如何通過url讀取文件08-24
Java如何讀取圖片EXIF信息02-20
Java如何讀取Jar中的資源01-29
Java如何讀取文本文件12-12
Java中如何高效的讀取大文件01-19
從Java的jar文件中如何讀取數(shù)據(jù)的方法01-24
Java讀取郵件的方法02-02
java讀取CSV的方法10-05