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); } }
相关文章推荐
- java读取图片的(尺寸、拍摄日期、标记)等EXIF信息
- java按指定编码写入和读取文件内容的类
- android开源项目SmartImageView设置网络图片url时抛出java.io.FileNotFoundException
- java按指定编码写入和读取文件内容的类
- Java从网络读取图片并保存至本地
- Java从网络读取图片并保存至本地 .
- java按指定编码写入和读取文件内容的类
- java 读取网络图片
- JAVA中读取网络中的图片资源导入到EXCEL中
- java 读取网络图片的大小(宽+高)
- java读取写入图片文件、byte[]、base64字符串、16进制字符串相互转换
- [转] JAVA中读取网络中的图片资源导入到EXCEL中
- java IO 使用Properties设置键值对配置信息 写入文件和从文件中读取
- Java从网络读取图片并保存至本地
- java字节流和字符流操作文件,指定编码写入和读取,递归创建上层目录
- java按指定编码写入和读取文件内容的类
- Java从网络读取图片并保存至本地
- Java从网络批量读取图片并保存至本网站服务器后再插入文章中
- Java使用URL读取网络图片资源 并保存在本地
- Java读取网络图片并存储