java实现base64字符串转换成图片保存

时间:2021-08-19 21:49:47
现在不管是博客论坛还是企业办公,都离不开资源的共享。通过文件上传的方式,与大家同分享,从而达到大众间广泛的沟通和交流,我们既可以从中获得更多的知识和经验,也能通过他人的反馈达到自我改进和提升的目的。   
下面介绍文件如何从本地发送到服务器的
 



import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;public class Base64Test {    public static void main(String[] args)    {        String strImg = GetImageStr();        System.out.println(strImg);        GenerateImage(strImg);    }    //图片转化成base64字符串    public static String GetImageStr()    {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理        String imgFile = "D:\\360CloudUI\\tupian\\jt.jpg";//待处理的图片        InputStream in = null;        byte[] data = null;        //读取图片字节数组        try         {            in = new FileInputStream(imgFile);                    data = new byte[in.available()];            in.read(data);            in.close();        }         catch (IOException e)         {            e.printStackTrace();        }        //对字节数组Base64编码        BASE64Encoder encoder = new BASE64Encoder();        return encoder.encode(data);//返回Base64编码过的字节数组字符串    }        //base64字符串转化成图片    public static boolean GenerateImage(String imgStr)    {   //对字节数组字符串进行Base64解码并生成图片        if (imgStr == null) //图像数据为空            return false;        BASE64Decoder decoder = new BASE64Decoder();        try         {            //Base64解码            byte[] b = decoder.decodeBuffer(imgStr);            for(int i=0;i<b.length;++i)            {                if(b[i]<0)                {//调整异常数据                    b[i]+=256;                }            }            //生成jpeg图片            String imgFilePath = "D:\\360CloudUI\\tupian\\new.jpg";//新生成的图片            OutputStream out = new FileOutputStream(imgFilePath);                out.write(b);            out.flush();            out.close();            return true;        }         catch (Exception e)         {            return false;        }    }}
 
package zPicture;public class Picture {public static final String DATA_IMAGE_PATH = PathUtil.getProjectPath(Picture.class, "zMP3").replace("zMP3","TEST/images/");public static boolean  test() {System.out.println(DATA_IMAGE_PATH);String imageName = null;try {imageName = CustomStringUtil.getExtension();//String imageName="";//图片存储路径String imagePath = DATA_IMAGE_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xVofg7QfAtvpcq3s+j6t4t8IT6FY3U9hrcWqN/PNF+2N+23faw3w\nZ0/9kzxh/wALvE6eHH0OP4b/ABEbxbB4jczWqq3gJtPF5BqizK0gtri3+zJOsjyWBt1MNb8ReHmd\n5VUy2NHDSqxxOAwkptLl5MTKK9vSfM1ecKjkpQXvx91uPLNM4vCj6UPhxx9hOMK9TO8LgqmQ8S51\ng4QqTUvrOT4fF1YZbmtN03K+Gx2FjTrQrv8AdOTnShVqVKde/wB7fs5aZ8Q/29/+Ck/wD/ZU+DWi\n6nJ8O/gf8YPBXxi/aO8exxMum6Z4a+FPi7TPEWoWU12QY7SK81LT4/CWgwsfter+LNRt7mKJdO04\n3g/0Rq/I3/gij+wzqn7Df7DXgfRfid4U0vRf2kPi9caj8V/2idVS2spPEs/izxNql/qXh/wh4i1m\nAzSX8vw88M3en+HZLOO7n0qy15PEd1pjOdQvL24/XKv6Q8PuF4cLcP0sPJNYvGKji8bdNONaVK3s\nmnZp01FqV1dSbjtG7/yB+lV41VfGrxRx+YYZwfD3DUsbkHDc4SUo4vBUsfiHVzPmjKUeXHzpwqUl\nFyX1eNCXNzTnFFFFFfcH80BRRRQAUUUUAFFFFABRgZz36Z744/wH6dSMkooAKKKKAP/Z\n";String imageType="jpg";//将字符串转化成图片boolean isSuccess = CustomStringUtil.base64StringToImage(imageBase64, imagePath,imageType);System.out.println(isSuccess);return isSuccess;} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return  false;}public static void main(String[] args) {test();}}


1.测试类

package zPicture;

public class PathUtil {
 /**
  * 获取工程路径
  * @param T
  * @param projectName
  * @return
  */
 public static String getProjectPath(Class<?> T,String projectName){
  String path = T.getResource("/").toString();
     System.out.println(path);
  path = path.substring(0,path.indexOf(projectName))+projectName;
     System.out.println(projectName);
     System.out.println(path);
     System.out.println(path.charAt(1));
     System.out.println(path.indexOf(":"));//4
  path = path.substring(path.indexOf(":")+1,path.length());
     System.out.println(path);// /D:/workspace2/zMP3
  
//  file:/D:/workspace2/zMP3/build/classes/
//  zMP3
//  file:/D:/workspace2/zMP3
//  path+="/WEB-INF/classes/setting.properties";
  path = path.replaceAll("%20", " ");
  System.out.println(path);
  return path;
 }

}

2.工具类

3.工具类(2)

package zPicture;

import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Calendar;

import javax.imageio.ImageIO;

import org.apache.commons.lang3.StringUtils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class CustomStringUtil {
 /**
  * 输出异常
  *
  * @param e
  * @return
  */
 public static String getErrorInfoFromException(Exception e) {
  try {
   StringWriter sw = new StringWriter();
   PrintWriter pw = new PrintWriter(sw);
   e.printStackTrace(pw);
   return "\r\n" + sw.toString() + "\r\n";
  } catch (Exception e2) {
   return "bad getErrorInfoFromException";
  }
 }

 /**
  * 图片转字符串
  * @return
  */
 public static String getImageBinary(String imagePath,String imageType) {
  BASE64Encoder encoder = new sun.misc.BASE64Encoder();
  File f = new File(imagePath);
  BufferedImage bi;
  try {
   bi = ImageIO.read(f);
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   ImageIO.write(bi, imageType, baos);
   byte[] bytes = baos.toByteArray();

   return encoder.encodeBuffer(bytes).trim();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return null;
 }
 
 /**
  * 字符串转图片
  * @param base64String
  */
 public static boolean base64StringToImage(String base64String,String toImagePath,String imageType) {
  try {
   BASE64Decoder decoder = new sun.misc.BASE64Decoder();
   byte[] bytes1 = decoder.decodeBuffer(base64String);

   ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
   RenderedImage bi1 = ImageIO.read(bais);
   File w2 = new File(toImagePath);// 可以是jpg,png,gif格式
   if(!w2.exists()){
    w2.createNewFile();
    System.out.println("no exist=====");
   }
   System.out.println("pass...........");
   return ImageIO.write(bi1, imageType, w2);// 不管输出什么格式图片,此处不需改动
  } catch (IOException e) {
   e.printStackTrace();
   return false;
  }
 }
 
 /**
  * 获取时间随机数
  * @return
  * @throws Exception
  */
 public static String getExtension()throws Exception{
  try {
   //线程睡会
   Thread.sleep(1);
   //生成日期实体
   Calendar calendar = Calendar.getInstance();
   String extension = calendar.getTime().getTime()+"";
   return extension;//专网及EZVP公网
  } catch (InterruptedException e) {
   e.printStackTrace();
   return null;
  }
 }
 
 /**
  * 将数组转换为字符串
  * @param array 数组对象
  * @param separator 分割字符
  * @return
  * @throws Exception
  */
 public static String getArrayToString(Object[] array, String separator)throws Exception{
  String resultStr = StringUtils.join(array, separator);
  return resultStr;
 }
 }
}

 

 

 

附加一篇转载帮助大家更好的理解:下面可实现图片与字符串互转

package zPicture;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class Base64Test
{
    public static void main(String[] args)
    {
        String strImg = GetImageStr();
        System.out.println(strImg);
        GenerateImage(strImg);
    }
    //图片转化成base64字符串
    public static String GetImageStr()
    {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
        String imgFile = "D:\\360CloudUI\\tupian\\jt.jpg";//待处理的图片
        InputStream in = null;
        byte[] data = null;
        //读取图片字节数组
        try
        {
            in = new FileInputStream(imgFile);       
            data = new byte[in.available()];
            in.read(data);
            in.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        //对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);//返回Base64编码过的字节数组字符串
    }
   
    //base64字符串转化成图片
    public static boolean GenerateImage(String imgStr)
    {   //对字节数组字符串进行Base64解码并生成图片
        if (imgStr == null) //图像数据为空
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try
        {
            //Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for(int i=0;i<b.length;++i)
            {
                if(b[i]<0)
                {//调整异常数据
                    b[i]+=256;
                }
            }
            //生成jpeg图片
            String imgFilePath = "D:\\360CloudUI\\tupian\\new.jpg";//新生成的图片
            OutputStream out = new FileOutputStream(imgFilePath);   
            out.write(b);
            out.flush();
            out.close();
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
    }
}

 附加一篇转载文件帮助大家理解图片转成字符串,下面介绍的是图片base64编码与解码代码

package zPicture;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;public class Base64Test {    public static void main(String[] args)    {        String strImg = GetImageStr();        System.out.println(strImg);        GenerateImage(strImg);    }    //图片转化成base64字符串    public static String GetImageStr()    {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理        String imgFile = "D:\\360CloudUI\\tupian\\jt.jpg";//待处理的图片        InputStream in = null;        byte[] data = null;        //读取图片字节数组        try         {            in = new FileInputStream(imgFile);                    data = new byte[in.available()];            in.read(data);            in.close();        }         catch (IOException e)         {            e.printStackTrace();        }        //对字节数组Base64编码        BASE64Encoder encoder = new BASE64Encoder();        return encoder.encode(data);//返回Base64编码过的字节数组字符串    }        //base64字符串转化成图片    public static boolean GenerateImage(String imgStr)    {   //对字节数组字符串进行Base64解码并生成图片        if (imgStr == null) //图像数据为空            return false;        BASE64Decoder decoder = new BASE64Decoder();        try         {            //Base64解码            byte[] b = decoder.decodeBuffer(imgStr);            for(int i=0;i<b.length;++i)            {                if(b[i]<0)                {//调整异常数据                    b[i]+=256;                }            }            //生成jpeg图片            String imgFilePath = "D:\\360CloudUI\\tupian\\new.jsp";//新生成的图片            OutputStream out = new FileOutputStream(imgFilePath);                out.write(b);            out.flush();            out.close();            return true;        }         catch (Exception e)         {            return false;        }    }}