现在的位置: 首页 > 综合 > 正文

java 获取网页(络)图片并保存

2014年02月02日 ⁄ 综合 ⁄ 共 859字 ⁄ 字号 评论关闭


package test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class SaveImage {

 public static void main(String[] args) {
  String path = "C:\\XunleiImageCache\\testimage.jpg";// 图片保存路径
  File file = new File(path);
  OutputStream os = null;
  String str = null;
  InputStream is = null;
  HttpURLConnection connection = null;
  URL userver = null;
  str = "http://www.baidu.com/img/baidu_sylogo1.gif";//百度首页图片网络路径
  try {
   userver = new URL(str);
   connection = (HttpURLConnection) userver.openConnection();
   connection.connect();
   is = connection.getInputStream();
   os = new FileOutputStream(file);
   int b = is.read();
   while (b != -1) {
    os.write(b);
    b = is.read();
   }
   is.close();
   os.close();
   System.out.println("图片testimage.jpg保存成功");
  } catch (Exception e) {
   e.printStackTrace();
   System.out.println(e);
  }
 }

}
原文链接:http://www.myexception.cn/program/1069475.html

抱歉!评论已关闭.