package com.yihu.jw.utils; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; /** * @ClassName ImageConvertUtil * @Description TODO * @Author heqian * @Date 2019/5/16 17:05 * @Version 1.0 */ public class ImageConvertUtil { static BASE64Encoder encoder = new BASE64Encoder(); static BASE64Decoder decoder = new BASE64Decoder(); static String upload_home = System.getProperty("catalina.home")+"resources/repository/"; public static void main(String[] args) { String str = getImageBinary("C://Users//Administrator//Desktop//医生信息//陈兰//资格证.jpg"); System.out.println(str); } public static String getImageBinary(){ File f = new File("d://123.jpg"); BufferedImage bi; try { bi = ImageIO.read(f); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(bi, "jpg", baos); byte[] bytes = baos.toByteArray(); return encoder.encodeBuffer(bytes).trim(); } catch (IOException e) { e.printStackTrace(); } return null; } public static String getImageBinary(String path){ File f = new File(path); BufferedImage bi; try { bi = ImageIO.read(f); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(bi, "jpg", baos); byte[] bytes = baos.toByteArray(); return encoder.encodeBuffer(bytes).trim(); } catch (IOException e) { e.printStackTrace(); } return null; } public static void base64StringToImage(String base64String, String fileName){ try { byte[] bytes1 = decoder.decodeBuffer(base64String); ByteArrayInputStream bais = new ByteArrayInputStream(bytes1); BufferedImage bi1 =ImageIO.read(bais); File w2 = new File(upload_home + fileName);//鍙互鏄痡pg,png,gif鏍煎紡 ImageIO.write(bi1, "jpg", w2);//涓嶇杈撳嚭浠�涔堟牸寮忓浘鐗囷紝姝ゅ涓嶉渶鏀瑰姩 } catch (IOException e) { e.printStackTrace(); } } }