62,614
社区成员
发帖
与我相关
我的任务
分享
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
public class GetImage {
public static void main(String[] args) {
try {
String url = "http://www.google.com.hk/intl/zh-CN/images/logo_cn.gif";
BufferedInputStream in = new BufferedInputStream(new URL(url).openStream());
File img = new File("google.gif");
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(img));
byte[] buf = new byte[2048];
int length = in.read(buf);
while(length != -1) {
out.write(buf,0,length);
length = in.read(buf);
}
in.close();
out.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}