Java学习笔记之网络编程基础-通过URL获取网络图片
package com.kkoolerter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URL;
public class GetImageByURL {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
URL url = new URL(
"http://hiphotos.baidu.com/baidu/pic/item/697e2a3fb95157a655e7234c.jpg");
DataInputStream dis = new DataInputStream(url.openStream());
OutputStream os = new FileOutputStream(new File("download.jpg"));
byte buffer[] = new byte[1024];
int len = -1;
while((len = dis.read(buffer))!=-1){
os.write(buffer, 0, len);
}
os.close();
dis.close();
}
}
©著作权归作者所有:来自51CTO博客作者juxiangwu的原创作品,如需转载,请注明出处,否则将追究法律责任