java压缩图片

一、导入下列jar包


           net.coobird
           thumbnailator
           0.4.8

二、代码

package com.centaline.answer.utils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import net.coobird.thumbnailator.Thumbnails;

public class ImageUtil {

    public static void compress(String original, String now) throws IOException {
                // 不压缩gif图
        if (original.toLowerCase().indexOf("gif") >= 0) {
            File file = new File(original);
            File newFile = new File(now);
            Files.copy(file.toPath(), newFile.toPath());
        } else {
            Thumbnails.of(original).scale(0.5f).outputQuality(0.5f).toFile(now);
        }

    }
}

你可能感兴趣的:(java压缩图片)