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

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文件格式

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();
        }
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值