/*
    * @author i柚
    * @description Java改变图片大小
    * @date 2022/2/22 11:17
    * @param [uploadFile, target, newWidth, newHeight]
    * @return void
    **/
    public void uploadImage(@RequestParam MultipartFile uploadFile, String target, int newWidth, int newHeight) {
        try {
            InputStream in = uploadFile.getInputStream();
            String filename = uploadFile.getOriginalFilename();
            assert filename != null;
            BufferedImage bi = ImageIO.read(in);
            BufferedImage image = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_BGR);
            Graphics graphics = image.createGraphics();
            graphics.drawImage(bi, 0, 0, newWidth, newHeight, null);
            DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(target));
            ImageIO.write(image, filename.substring(filename.lastIndexOf(".") + 1), dataOutputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Q.E.D.


一蓑烟雨任平生