修改压缩不好使的问题
This commit is contained in:
parent
6983d37a86
commit
7b105d4dfc
|
@ -111,22 +111,39 @@ public class FileUploadUtils
|
|||
String fileName = extractFilename(file);
|
||||
|
||||
File desc = getAbsoluteFile(baseDir, fileName);
|
||||
|
||||
// try {
|
||||
// byte[] compressedImage = compressImage(desc, 0.5f); // 压缩比例,如0.5表示压缩50%
|
||||
// // 将压缩后的图片保存到文件
|
||||
// ImageIO.write(ImageIO.read(new ByteArrayInputStream(compressedImage)), "png", desc);
|
||||
// } catch (IOException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
//
|
||||
OutputStream out = Files.newOutputStream(desc.toPath());
|
||||
IoUtil.copy(file.getInputStream(), out);
|
||||
//file.transferTo(desc);
|
||||
IoUtil.close(out);
|
||||
String pathFileName = getPathFileName(baseDir, fileName);
|
||||
try {
|
||||
File inputImageFile = new File(pathFileName); // 替换为实际图片路径
|
||||
byte[] compressedImage = compressImage(inputImageFile, 0.5f); // 压缩比例,如0.5表示压缩50%
|
||||
|
||||
// 将压缩后的图片保存到文件
|
||||
File outputFile = new File(pathFileName); // 替换为输出图片路径
|
||||
ImageIO.write(ImageIO.read(new ByteArrayInputStream(compressedImage)), "png", outputFile);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// try {
|
||||
// File input = new File(baseDir+"/"+fileName);
|
||||
// BufferedImage image = ImageIO.read(input);
|
||||
//// int width = image.getWidth();
|
||||
//// int height = image.getHeight();
|
||||
// double scaleFactor = 0.1; // 压缩比例为50%
|
||||
// int width = (int) (image.getWidth() * scaleFactor);
|
||||
// int height = (int) (image.getHeight() * scaleFactor);
|
||||
//
|
||||
// BufferedImage compressedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
// Graphics2D g2d = compressedImage.createGraphics();
|
||||
// g2d.drawImage(image, 0, 0, width, height, null);
|
||||
// g2d.dispose();
|
||||
// ImageIO.write(compressedImage, "png", input);
|
||||
//
|
||||
// System.out.println("压缩成功!");
|
||||
// } catch (Exception e) {
|
||||
// System.out.println("压缩失败:" + e.getMessage());
|
||||
// }
|
||||
return pathFileName;
|
||||
}
|
||||
public static byte[] compressImage(File imageFile, float scale) throws IOException {
|
||||
|
|
Loading…
Reference in New Issue