您的位置:首页 > 理论基础 > 计算机网络

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

2017-07-03 00:00 399 查看
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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息