java 读取网络图片并设置指定尺寸写入

package com.boya.util;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;

import static java.lang.System.out;

/**
 * Created by nrj on 2017/7/3.
 */
public class ImageUrlDow {


    //获取图片   写入图片
    public static void getDoc(String httpUrl,int imgWidth,int imgHeight) throws IOException{

        //定义一个写入的url
        File f = new File("E://imgs");
        if (!f.exists()){//如果没有此文件夹  就创建
            f.mkdirs();
        }
        //定义一个获取图片的http地址  获取http的内容   ignoreContentType就是忽略ContextType的检查
        Document document = Jsoup.connect(httpUrl).ignoreContentType(true).get();
        //获取后缀为png和jpg的图片的元素集合
        Elements imgs = document.select("img[src~=(?i)\\.(png|jpe?g)]");
        for (Element element:imgs){
            //获取到图片的地址
            String src = element.attr("src");
            //获取图片的后缀名字
            String imageName = src.substring(src.lastIndexOf("/"),src.length());
            //判断有没有http://请求头(百度没有) 要强制添加上
            Boolean s = src.contains("http");
            if (s == false){
                src = "http:"+src;
            }
            //连接url
            URL url = new URL(src);
            URLConnection uri = url.openConnection();
            //获取数据流
            InputStream inputStream = uri.getInputStream();
            //写入流
            OutputStream outputStream = new FileOutputStream(new File("E://imgs" ,imageName));

            Image img = ImageIO.read(inputStream);
            BufferedImage image = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_BGR);
            image.getGraphics().drawImage(img, 0, 0, imgWidth, imgHeight, null);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream);
            encoder.encode(image);
            outputStream.flush();
            inputStream.close();
            outputStream.close();
        }

    }
    public static void main(String[] args) throws IOException{
        new ImageUrlDow().getDoc("https://www.baidu.com",300,180);
    }
}

转载于:https://my.oschina.net/niuruijing/blog/1142838

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值