base64加密解密圖片(利用java.util包)
阿新 • • 發佈:2018-12-13
package iis.utils; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.imageio.ImageIO; import java.util.Base64; public class ImageUtil { /** * 解密 * * @param imageString * @return */ public static BufferedImage decodeToImage(String imageString) { BufferedImage image = null; byte[] imageByte; try { imageByte = Base64.getDecoder().decode(imageString); ByteArrayInputStream bis = new ByteArrayInputStream(imageByte); image = ImageIO.read(bis); bis.close(); } catch (Exception e) { e.printStackTrace(); } return image; } /** * 加密 * * @param image * @param type * @return */ public static String encodeToString(BufferedImage image, String type) { String imageString = null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { ImageIO.write(image, type, bos); byte[] imageBytes = bos.toByteArray(); imageString = Base64.getEncoder().encodeToString(imageBytes); bos.close(); } catch (IOException e) { e.printStackTrace(); } return imageString; } }