输入一个网络图片地址,可以把图片保存到本地
这段代码我看的不是很明白,谁大概说下,具体怎么实现的
尤其是两段while 循环 起什么作用public class TestFind {
public boolean saveUrlAs(String photoUrl, String fileName) {
try {
URL url = new URL(photoUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
DataInputStream in = new DataInputStream(connection
.getInputStream());
DataOutputStream out = new DataOutputStream(new FileOutputStream(
fileName));
byte[] buffer = new byte[4096];
int count = 0;
while ((count = in.read(buffer)) > 0) {
out.write(buffer, 0, count);
}
out.close();
in.close();
return true;
} catch (Exception e) {
return false;
}
} public String getDocumentAt(String urlString) {
StringBuffer document = new StringBuffer();
try {
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
document.append(line + "\n");
}
reader.close();
} catch (MalformedURLException e) {
System.out.println("Unable to connect to URL: " + urlString);
} catch (IOException e) {
System.out.println("IOException when connecting to URL: "
+ urlString);
}
return document.toString();
}public static void main(String[] args) throws IOException {
TestFind test = new TestFind();
String photoUrl = "http://www.baidu.com/img/baidu_logo.gif";
String fileName = photoUrl.substring(photoUrl.lastIndexOf("/"));
String filePath = "d:/";
boolean flag = test.saveUrlAs(photoUrl, filePath+fileName );
System.out.println("Run ok!\nGet URL file " + flag);
}}
这段代码我看的不是很明白,谁大概说下,具体怎么实现的
尤其是两段while 循环 起什么作用public class TestFind {
public boolean saveUrlAs(String photoUrl, String fileName) {
try {
URL url = new URL(photoUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
DataInputStream in = new DataInputStream(connection
.getInputStream());
DataOutputStream out = new DataOutputStream(new FileOutputStream(
fileName));
byte[] buffer = new byte[4096];
int count = 0;
while ((count = in.read(buffer)) > 0) {
out.write(buffer, 0, count);
}
out.close();
in.close();
return true;
} catch (Exception e) {
return false;
}
} public String getDocumentAt(String urlString) {
StringBuffer document = new StringBuffer();
try {
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
document.append(line + "\n");
}
reader.close();
} catch (MalformedURLException e) {
System.out.println("Unable to connect to URL: " + urlString);
} catch (IOException e) {
System.out.println("IOException when connecting to URL: "
+ urlString);
}
return document.toString();
}public static void main(String[] args) throws IOException {
TestFind test = new TestFind();
String photoUrl = "http://www.baidu.com/img/baidu_logo.gif";
String fileName = photoUrl.substring(photoUrl.lastIndexOf("/"));
String filePath = "d:/";
boolean flag = test.saveUrlAs(photoUrl, filePath+fileName );
System.out.println("Run ok!\nGet URL file " + flag);
}}
HttpURLConnection标示应用程序和资源之间的通信链接。
connection.getInputStream()获取从此打开的连接读取的输入流,之后你便可以从流中读取你要想的东西。
public boolean saveUrlAs(String photoUrl, String fileName) {try {
URL url = new URL(photoUrl);//构造url
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();//建立链接
DataInputStream in = new DataInputStream(connection
.getInputStream());//得到输入流
DataOutputStream out = new DataOutputStream(new FileOutputStream(
fileName)); //创建输出流
byte[] buffer = new byte[4096];//字节数组(大小4k)
int count = 0;
while ((count = in.read(buffer)) > 0) {//输入流(io)循环读入buffer
out.write(buffer, 0, count);//写入
}
out.close();//关闭输出流
in.close();//关闭输入流
return true;
} catch (Exception e) {
return false;
}
}public String getDocumentAt(String urlString) {StringBuffer document = new StringBuffer();
try {
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {//读一行内容,直到为null(读完)
document.append(line + "\n");
}
reader.close();
} catch (MalformedURLException e) {
System.out.println("Unable to connect to URL: " + urlString);
} catch (IOException e) {
System.out.println("IOException when connecting to URL: "
+ urlString);
}
return document.toString();
}public static void main(String[] args) throws IOException {
TestFind test = new TestFind();
String photoUrl = "http://www.baidu.com/img/baidu_logo.gif";
String fileName = photoUrl.substring(photoUrl.lastIndexOf("/"));
String filePath = "d:/";
boolean flag = test.saveUrlAs(photoUrl, filePath+fileName );
System.out.println("Run ok!\nGet URL file " + flag);
}}
out.write(buffer, 0, count);
}读入buffer,再从buffer写出,终点是文件对象
out.write(buffer, 0, count);
}读入buffer,再从buffer写出,终点是文件对象
一点一点,来做,最后就成了一个文件了。文件也是由二进制组成的。
public boolean saveUrlAs(String photoUrl, String fileName) { try
{
//类 URL 代表一个统一资源定位符,它是指向互联网“资源”的指针。资源可以是简单的文件或目录
URL url = new URL(photoUrl);
//与想要获取的资源建立连接,并可以根据该连接获得输入或输出流
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
//数据输入流
DataInputStream in = new DataInputStream(connection
.getInputStream());
//数据输出流
DataOutputStream out = new DataOutputStream(new FileOutputStream(
fileName));
byte[] buffer = new byte[4096];
int count = 0;
//以字节的形式读取图片到buffer数组,当图片读取完毕时count 就等于-1;这句代码就是说每次循环都往buffer里面填充了count个byte.
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);//输出图片
}
out.close();
in.close();
return true;
} catch (Exception e) {
return false;
}
} public String getDocumentAt(String urlString) { StringBuffer document = new StringBuffer();
try {
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line = null;
//一般用来读取文本文件,每循环一次读取一行,为null时表示已读到文件末尾
while ((line = reader.readLine()) != null) {
document.append(line + "\n");
}
reader.close();
} catch (MalformedURLException e) {
System.out.println("Unable to connect to URL: " + urlString);
} catch (IOException e) {
System.out.println("IOException when connecting to URL: "
+ urlString);
}
return document.toString();
}
}