Chinaunix首页 | 论坛 | 博客
  • 博客访问: 79685
  • 博文数量: 31
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 839
  • 用 户 组: 普通用户
  • 注册时间: 2013-03-12 14:38
个人简介

我在收集一些石头,我期待它们有一天能够串联成一串项链。

文章分类

全部博文(31)

文章存档

2013年(31)

我的朋友

分类: Java

2013-03-12 16:54:22

简单图片抓取,Java实现

点击(此处)折叠或打开

  1. public void getpic(String src,String num,int loc,String alt,String category,String name) {
  2.         int id = Integer.parseInt(num);
  3.         int id1 = id/1000000;
  4.         int id2 = id/1000;
  5.         String path ="F:\\image\\"+id1+"\\"+id2+"\\"+num+"\\";
  6.         File cate = new File(path);
  7.         if (!cate.exists()&&!cate.isDirectory()) {
  8.             cate.mkdirs();
  9.         }
  10.         String target = path+loc+".png";
  11.         File file = new File(target);
  12.         //System.out.println("url "+src);

  13.         //System.out.println(target);

  14.         try {
  15.             URL url = new URL(src);
  16.             HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  17.             InputStream inputStream=httpURLConnection.getInputStream();
  18.             byte[] bs = new byte[1024];
  19.             int len;
  20.             OutputStream outputStream = new FileOutputStream(file, true);
  21.             while ((len=inputStream.read(bs))!=-1) {
  22.                 outputStream.write(bs, 0, len);
  23.             }
  24.             outputStream.close();
  25.             inputStream.close();
  26.             httpURLConnection.disconnect();
  27.             BufferedImage bufferedImage = ImageIO.read(file);
  28.             int width = bufferedImage.getWidth();
  29.             int height = bufferedImage.getHeight();
  30.                     } catch (Exception e) {
  31.             // TODO: handle exception

  32.              //e.printStackTrace();

  33.             return ;
  34.         }
  35.     }

简单实例,保存时可修改格式。
阅读(661) | 评论(0) | 转发(0) |
0

上一篇:网页解析

下一篇:简单表达式翻译器

给主人留下些什么吧!~~