==========
===========
package com.proli.util; import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException; import com.sun.org.apache.xml.internal.security.utils.Base64; import com.sun.xml.internal.messaging.saaj.util.ByteInputStream; import org.springframework.util.Base64Utils; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.UUID; public class Base64AndImg { static String base64Str = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcU " + "FhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgo " + "KCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAAeAFoDASIA " + "AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA " + "AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3 " + "ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm " + "p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA " + "AwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx " + "BhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK " + "U1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3 " + "uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6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x8kDegLdjjlwCDnHA+HPCPiDS7NdFv8AwH4S1OeJSkOtymJYiNmV8xPLMjkH " + "5ScKT37ue+fxhp+k6E01zDdMtnbb5BGqkkIuTjLD0q1P4k0y4QJPazSoGVwrxqwDKwZTyeoIBB7E " + "A0AbGjQS22j2MFzFawzxQRpJHaKVhRgoBEYPIQHoPTFWLeNoreKOSZ53RQrSyBQzkD7x2gDJ68AD " + "0ArD/wCErsf+eVz/AN8r/jUcviXT5Hhdo70GJt67TtBO0r8wDYYYY8HIzg9QCADh/wBnvRNTsPBt " + "zb6ta3VgP7ReUwzxSQSuQsBRgcg7MqwIwQ2cHgEH1i4giubeWC5iSaCVSkkcihldSMEEHggjtXHy " + "+PdMfUIbdV1GN0vPszBY49sjfZzLhsknbgg5GDuUds50LfxPZxW8Ucn22d0UK0siIGcgfeO3Ayev " + "AA9AKALGq+HotR8U6FrUs7o+krcCOJQMO0qquWPoAG4HUkc8YNOPwra2Pjq98WW6zyXl5axWckEZ " + "GD86hpSWPZVj4GOEbhmYCjUfG2nWGn3N5NBdtFbxNKwVFJIUEnHzdeKB4mge0hSaS5W4XYZJYIUU " + "OQQWAVmbCtgjGSQDwc4NAHUV4foPw7e60fxrb3FhOP3s2j6NBMGiEFuJzMkm8kGSPzHVyTuOIyBu " + "ztr1D/hK7H/nlc/98r/jUdv4k0y2t4oLa1mhgiUJHHHGqqigYAABwAB2oAy/g3ZXVr4IiudVtp7f " + "V9Quri8vhPGY3eZpWG4ocbcqqcAAd8c1m6t/yFb3/rs//oRrqv8AhK7H/nlc/wDfK/41yN9Ks97c " + "TICFkkZwD1wTmgD/2Q=="; public static void base64ToImg(String base64Str, String filePath, String fileName) throws IOException, Base64DecodingException { byte[] decode = Base64.decode(base64Str); BufferedImage read = ImageIO.read(new ByteInputStream(decode, decode.length)); if (fileName == null) { fileName = UUID.randomUUID().toString().replaceAll("-", "") + ".jpg"; } File file = new File(filePath + fileName); ImageIO.write(read, "JPG",file); } public static String imgToBase64(File imgFile) throws IOException { BufferedImage read = ImageIO.read(imgFile); return imgBufferedToBase64String(read); } public static String imgToBase64(URL imgUrl) throws IOException { BufferedImage read = ImageIO.read(imgUrl); return imgBufferedToBase64String(read); } public static String imgBufferedToBase64String(BufferedImage bufferedImage) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ImageIO.write(bufferedImage, "jpg", byteArrayOutputStream); byte[] bytes = byteArrayOutputStream.toByteArray(); return Base64Utils.encodeToString(bytes); } public static void main(String[] args) throws IOException, Base64DecodingException { //base64ToImg(base64Str,"F:\","123.jpg"); //File file = new File ("F:\123.jpg"); URL imgUrl = new URL("http://iir.circ.gov.cn/web/servlet/ValidateCode?time=123"); String base64Str = imgToBase64(imgUrl); base64ToImg(base64Str,"F:\",null); } }