小言_互联网的博客

根据excel中地址下载并设置图片尺寸大小

259人阅读  评论(0)

1、引入pom依赖

1、1 使用hutool工具包进行excel导入,并解析

		<dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.0.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.17</version>
        </dependency>

2、构建excel对应的模型

2.1、依赖lombok

package com.suy.actuator.model;

import lombok.Data;

@Data
public class ImgBean{

    private String name;
    private String imgUrl;
}

2.2、excel文件格式

3、图片下载及尺寸修改

package com.suy.actuator.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.suy.actuator.model.ImgBean;
import org.junit.jupiter.api.Test;

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

public class DownloadImg {

    @Test
    public void downloadImg() {
        ExcelReader excelReader = ExcelUtil.getReader(FileUtil.file("imgExcel.xlsx"));
        List<ImgBean> imgs = excelReader.readAll(ImgBean.class);
        for (ImgBean img : imgs ) {
            download(img.getImgUrl(), img.getName());
        }
    }

    /**
     * 下载并修改图片尺寸
     * @param urlString
     * @param rename
     */
    public static void download(String urlString, String rename) {
        // 构造URL
        InputStream is = null;
        OutputStream os = null;
        String oldPath = "D:/oldPath/" + rename + ".jpg";
        String newSizePaht = "D:/newPath/" + rename + ".jpg";
        try {
            URL url = new URL(urlString);
            // 打开连接
            URLConnection con = url.openConnection();
            // 输入流
            is = con.getInputStream();
            // 1K的数据缓冲
            byte[] bs = new byte[1024];
            // 读取到的数据长度
            int len;
            // 输出的文件流
            os = new FileOutputStream(new File(oldPath));
            // 开始读取
            while ((len = is.read(bs)) != -1) {
                os.write(bs, 0, len);
            }

            changeSize(800, 800, oldPath, newSizePaht);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(is, os);
        }
    }

    /**
     * 修改图片尺寸
     * @param newWidth 新的宽度
     * @param newHeight 新的高度
     * @param path 原图片路径
     * @param newSizePath 修改后的图片路径
     */
    public static void changeSize(int newWidth, int newHeight, String path, String newSizePath) {
        BufferedInputStream in = null;
        BufferedOutputStream out = null;
        try {
            in = new BufferedInputStream(new FileInputStream(path));

            //字节流转图片对象
            Image bi = ImageIO.read(in);
            //构建图片流
            BufferedImage tag = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
            //绘制改变尺寸后的图
            tag.getGraphics().drawImage(bi, 0, 0, newWidth, newHeight, null);
            //输出流
            out = new BufferedOutputStream(new FileOutputStream(newSizePath));
            ImageIO.write(tag, "jpg", out);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(in, out);
        }
    }

    /**
     * 关闭数据源
     * @param is
     * @param os
     */
    private static void close(InputStream is, OutputStream os) {
        // 完毕,关闭所有链接
        try {
            if (os != null) {
                os.close();
            }
            if (is != null) {
                is.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}


转载:https://blog.csdn.net/shine_yang914/article/details/102487436
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场