我试图在更改文件质量后替换文件 - 问题是我只能创建一个新文件,而不是替换原始文件。这是我正在使用的代码:
private static void resize(String folder, String filename)
throws IOException {
BufferedImage imagem = ImageIO.read(new File(folder + "/" + filename));
float quality = (float) 0.25;
ImageWriter writer = (ImageWriter) ImageIO.getImageWritersByFormatName(
"jpeg").next();
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(quality);
File file = new File(folder + "/" + filename);
FileImageOutputStream output = new FileImageOutputStream(file);
writer.setOutput(output);
IIOImage image = new IIOImage(imagem, null, null);
writer.write(null, image, iwp);
writer.dispose();
}