批量图片压缩 自己修改年份,其他都不用动

引入依赖:


        
            net.coobird
            thumbnailator
            0.4.8
        

代码:

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CompressController {
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
    /*

   oldsrc  : 原图片地址文件夹 如 'd:/'
   newsrc  : 压缩后图片地址文件夹 如 'e:/'
   widthdist,heightdist : 压缩后的宽和高

   createtime 2010-11-25
   @auto yijianfeng
 */
    @ResponseBody
    @GetMapping("/reduceImgAlls")
    public void reduceImgAlls() {
        for(int n=1;n<12;n++) {
            String stn="";
            if (n < 10) {
                stn = "/0" + n;
            } else {
                stn = "/" + n;
            }
            System.out.println(stn);
            for (int j = 1; j < 32; j++) {
//            if(j==24){
//                continue;
//            }
                String str = "";
                if (j < 10) {
                    str = "/0" + j;
                } else {
                    str = "/" + j;
                }
                String oldsrc = "C:/Users/kyx12/Desktop/files/upload/2019"+stn+str;
                String newsrc = "E:/img/upload/2019"+stn+str;

                try {
                    File file = new File(oldsrc);
                    if (!file.exists()) {
                        continue;
                    }
                    File[] srcfile = file.listFiles();
                    if (srcfile != null) {
                        for (int i = 0; i < srcfile.length; i++) {
                            if (srcfile[i].isFile()
                                    && (srcfile[i].getName().endsWith(".jpg")
                                    || srcfile[i].getName().endsWith(".png")
                                    || srcfile[i].getName().endsWith(".JPG")
                                    || srcfile[i].getName().endsWith(".gif") || srcfile[i]
                                    .getName().endsWith(".gif"))) {
                                //等比例缩放
                                Thumbnails.of(oldsrc + "/" + srcfile[i].getName()).scale(0.8f).toFile(newsrc
                                        + "/" + srcfile[i].getName());
                            } else {

                            }
                        }
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}

你可能感兴趣的:(批量图片压缩 自己修改年份,其他都不用动)