java 本地图片压缩 转base64并限制文件大小

  1. /**
    • 本地图片转base64并限制文件大小
    • @param imagePath 图片全路径
    • @param sizeLimit 大小 整数 限制的大小 KB 1024
    • @return 返回值为0,imageurl为空;返回值为1,imageurl路径不可访问;具体指为转换后的值
      */
      public static String convertLocalImageToBase64(String imagePath, Integer sizeLimit) {
      Log.debug(“[本地图片转base64]imagePath:{},sizeLimit:{}” + imagePath + sizeLimit);
      // 判断路径是否为空
      if (StrUtil.isBlank(imagePath)) {
      Log.debug(“传入的地址为空!”);
      return “0”;
      }
      // 默认上限为50k
      if (sizeLimit == null) {
      sizeLimit = 50;
      }
      sizeLimit = sizeLimit * 1024;
      String base64Image = null;
      DataInputStream dataInputStream = null;
      ByteArrayOutputStream outputStream = null;
      ByteArrayInputStream inputStream = null;
      try {
      FileInputStream fis = new FileInputStream(imagePath);
      dataInputStream = new DataInputStream(fis);
      outputStream = new ByteArrayOutputStream();
      byte[] buffer = new byte[2048];
      int length;
      while ((length = dataInputStream.read(buffer)) > 0) {
      outputStream.write(buffer, 0, length);
      }
      byte[] context = outputStream.toByteArray();
      Log.debug(“[本地图片转base64]byte context”);
      // 将图片数据还原为图片
      inputStream = new ByteArrayInputStream(context);
      BufferedImage image = ImageIO.read(inputStream);
      int imageSize = context.length;
      int type = image.getType();
      int height = image.getHeight();
      int width = image.getWidth();
      BufferedImage tempImage;
      // 判断文件大小是否大于size,循环压缩,直到大小小于给定的值
      Log.debug(“[imageSize]” + imageSize + " sizeLimit :" + sizeLimit);
      while (imageSize > sizeLimit) {
      Log.debug(“[循环压缩]” + imageSize + " ,sizeLimit :" + sizeLimit);
      // 将图片长宽压缩到原来的90%
      height = new Double(height * 0.9).intValue();
      width = new Double(width * 0.9).intValue();
      tempImage = new BufferedImage(width, height, type);
      // 绘制缩小后的图 //不执行也不报错需要配置下服务器tomcat启动配置 留在文章结尾
      Log.debug(“[tempImage]” + tempImage);
      tempImage.getGraphics().drawImage(image, 0, 0, width, height, null);
      // 重新计算图片大小
      outputStream.reset();
      boolean wriFlag = ImageIO.write(tempImage, “jpg”, outputStream);
      imageSize = outputStream.toByteArray().length;
      Log.debug(“[压缩后 imageSize]” + imageSize);
      }
      // 将图片转化为base64并返回
      byte[] data = outputStream.toByteArray();
      // 此处一定要使用org.apache.tomcat.util.codec.binary.Base64,防止再linux上出现换行等特殊符号
      base64Image = Base64.encodeBase64String(data);
      return base64Image;
      } catch (Exception e) {
      Log.error(“[本地图片转base64][异常]”, e);
      } finally {
      IoUtil.close(dataInputStream);
      IoUtil.close(outputStream);
      IoUtil.close(inputStream);
      }
      Log.debug(“[本地图片转base64]base64Image的长度:{}” + base64Image.length());
      return base64Image;
      }

//这个不执行困扰了好几天,找了很多说法也换了很多方法都不行,最后在tomcat启动配置项修改了下 竟然成功了

linux下tomcat的启动文件要设置JAVA_OPTS=“-Djava.awt.headless=true”
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用 Java 中的 `java.util.zip` 和 `java.util.Base64` 类来实现文件的压缩和换为 Base64 编码。以下是一个示例代码: ```java import java.io.*; import java.util.zip.DeflaterOutputStream; import java.util.zip.InflaterInputStream; import java.util.Base64; public class FileCompression { public static void main(String[] args) { String filePath = "path/to/your/file.txt"; // 替换为你的文件路径 String base64String = compressToBase64(filePath); System.out.println("Base64 编码: " + base64String); } public static String compressToBase64(String filePath) { try { File inputFile = new File(filePath); FileInputStream fileInputStream = new FileInputStream(inputFile); ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteOutputStream); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = fileInputStream.read(buffer)) != -1) { deflaterOutputStream.write(buffer, 0, bytesRead); } deflaterOutputStream.close(); fileInputStream.close(); byte[] compressedData = byteOutputStream.toByteArray(); byte[] base64EncodedData = Base64.getEncoder().encode(compressedData); return new String(base64EncodedData); } catch (IOException e) { e.printStackTrace(); } return null; } } ``` 在上面的示例代码中,我们首先通过 `FileInputStream` 读取文件内容,然后使用 `DeflaterOutputStream` 进行压缩。接下来,我们将压缩后的数据通过 `Base64.getEncoder().encode()` 方法进行 Base64 编码。最后,返回 Base64 编码后的字符串。 请确保将 `filePath` 替换为你要压缩的文件的实际路径。注意,这个示例只是演示了如何进行文件压缩和换为 Base64 编码,并未处理异常情况,你可能需要根据你的实际需求进行适当的异常处理

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值