精华内容
下载资源
问答
  • 用于利用Java程序将图片转换为Base64编码。简单易懂。
  • java将图片转换为base64格式

    千次阅读 2018-08-06 17:04:31
    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream;...import Decoder.BASE64Decode...
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    import Decoder.BASE64Decoder;
    import Decoder.BASE64Encoder;
    
    public class Base64 {
    
    	/*
    	 * 图片转化成base64字符串  
    	 * imagefilePath  图片路径
    	 */
        public static String GetImageStr(String imagefilePath)  
        {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理  
           
            InputStream in = null;  
            byte[] data = null;  
            //读取图片字节数组  
            try   
            {  
                in = new FileInputStream(imagefilePath);   //该路径只能是本机路径,不能是网络地址       
                data = new byte[in.available()];  
                in.read(data);  
            }   
            catch (IOException e)   
            {  
                e.printStackTrace();  
            } finally{
            	try {
            		if(in != null){
            			in.close();	
            		}
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
            }  
           // 对字节数组Base64编码  
            BASE64Encoder encoder = new BASE64Encoder();  
            return "data:image/png;base64,"+encoder.encode(data).replaceAll("\n|\r","");//返回Base64编码过的字节数组字符串
            
           
        }  
        
        /*
    	 *创建文件夹路径
    	 * imagefilePath  图片路径
    	 */
        public static void createMk(String imageMKPath)  
        {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理  
           
        	File file =new File(imageMKPath);    
        	//如果文件夹不存在则创建    
        	if  (!file .exists()  && !file .isDirectory())      
        	{       
        	    System.out.println("//不存在");  
        	    file .mkdir();    
        	} else   
        	{  
        	    System.out.println("//目录存在");  
        	}  
    
        }  
          
        
        /*
         * base64字符串转化成图片
         * imgStr 图片base64编码
         * newFilePath  生成图片指定路径
         */
        public static boolean GenerateImage(String imgStr, String newFilePath)  
        {   
      
        	//对字节数组字符串进行Base64解码并生成图片  
            if (imgStr == null) //图像数据为空  
                return false;  
            BASE64Decoder decoder = new BASE64Decoder();  
            OutputStream out = null;
            try   
            {  
                //Base64解码  
                byte[] b = decoder.decodeBuffer(imgStr);  
                for(int i=0;i<b.length;++i)  
                {  
                    if(b[i]<0)  
                    {//调整异常数据  
                        b[i]+=256;  
                    }  
                }  
              
                out = new FileOutputStream(newFilePath);      
                out.write(b);  
                out.flush();  
                  
                return true;  
            }   
            catch (Exception e)   
            {  
                return false;  
            }  finally {
            	if(out != null){
            		try {
    					out.close();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
            	}
            }
        }  
    }

     

    展开全文
  • 1、基本流程 2、图片转化成base64字符串 public static String ... {//将图片文件转化字节数组字符串,并对其进行Base64编码处理 InputStream in = null; byte[] data = null; //读取图片字节...

    1、基本流程

    2、图片转化成base64字符串

    public static String GetImageStr(String imgFile)  
        {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理  
            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 base64str,String savepath)  
        {   //对字节数组字符串进行Base64解码并生成图片  
            if (base64str == null) //图像数据为空  
                return false;  
           // System.out.println("开始解码");
            BASE64Decoder decoder = new BASE64Decoder();  
            try   
            {  
                //Base64解码  
                byte[] b = decoder.decodeBuffer(base64str);  
              //  System.out.println("解码完成");
                for(int i=0;i<b.length;++i)  
                {  
                    if(b[i]<0)  
                    {//调整异常数据  
                        b[i]+=256;  
                    }  
                }
               // System.out.println("开始生成图片");
                //生成jpeg图片  
                OutputStream out = new FileOutputStream(savepath);      
                out.write(b);  
                out.flush();  
                out.close();  
                return true;  
            }   
            catch (Exception e)   
            {  
                return false;  
            }  
        }
    展开全文
  • 主要介绍了Java实现base64图片编码数据转换为本地图片的方法,涉及java编码转换图片文件生成相关操作技巧,需要的朋友可以参考下
  • 本篇文章主要介绍了Java 处理图片base64 编码的相互转换的示例,具有一定的参考价值,有兴趣的可以了解一下
  • 主要大家详细介绍了java将图片转为base64返回给前端,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
  • java 把PDF转换BASE64

    2017-12-20 16:16:09
    java 把PDF转换成BASE64,java 把PDF转换成BASE64,java 把PDF转换BASE64,java 把PDF转换成BASE64java 把PDF转换成BASE64
  • JAVA 将图片转换为Base64编码

    千次阅读 2018-11-26 18:55:00
    这里使用的jar包是commons-codec-1.10.jar; import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;imp...

    https://www.cnblogs.com/ywlx/p/4544179.html

    这里使用的jar包是commons-codec-1.10.jar;

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;

    import org.apache.commons.codec.binary.Base64;


    /**
    * 将图片转换为Base64<br>
    * 将base64编码字符串解码成img图片
    * @创建时间 2015-06-01 15:50
    *
    */
    public class Img2Base64Util {

    public static void main(String[] args) {
    String imgFile = "d:\\3.jpg";//待处理的图片
    String imgbese=getImgStr(imgFile);
    System.out.println(imgbese.length());
    System.out.println(imgbese);
    String imgFilePath = "d:\\332.jpg";//新生成的图片
    generateImage(imgbese,imgFilePath);
    }
    /**
    * 将图片转换成Base64编码
    * @param imgFile 待处理图片
    * @return
    */
    public static String getImgStr(String imgFile){
    //将图片文件转化为字节数组字符串,并对其进行Base64编码处理


    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();
    }
    return new String(Base64.encodeBase64(data));
    }

    /**
    * 对字节数组字符串进行Base64解码并生成图片
    * @param imgStr 图片数据
    * @param imgFilePath 保存图片全路径地址
    * @return
    */
    public static boolean generateImage(String imgStr,String imgFilePath){
    //
    if (imgStr == null) //图像数据为空
    return false;

    try
    {
    //Base64解码
    byte[] b = Base64.decodeBase64(imgStr);
    for(int i=0;i<b.length;++i)
    {
    if(b[i]<0)
    {//调整异常数据
    b[i]+=256;
    }
    }
    //生成jpeg图片

    OutputStream out = new FileOutputStream(imgFilePath);
    out.write(b);
    out.flush();
    out.close();
    return true;
    }
    catch (Exception e)
    {
    return false;
    }
    }
    }

     

     







    public static void main(String[] args){
    try {
    base64UpLoad("data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCABaAIcDASIAAhEBAxEB/8QAHQAAAgIDAQEBAAAAAAAAAAAABgcEBQADCAkCAf/EAEMQAAEDAgQEAwUDCgQGAwAAAAECAwQFEQAGEiEHEzFBFCJRMmFxgZEII6EVFjNCUmKxweHwNEOi0SRygpKys8LD8f/EABkBAAMBAQEAAAAAAAAAAAAAAAABAgMEBf/EACkRAAICAQMDAgYDAAAAAAAAAAABAhEDEiExEyJBBFEUQlJhkaFigcH/2gAMAwEAAhEDEQA/AHfUqE+phwx6vKakL25qg2QL7XtosSBvb3dsQ6XQ5VLp6Yyq/UJKgpZLrqGNStSyq5s1137WHoB0xHkZvdKNRpUzp3Wxt9HMDdazxU0SojMSkKcDiitwvPoTobTa521Am6htf1wxhCMtyF11p5FcnNsqA5jKVIs6qxFz5bDoi1vQjFrJyXHcdHNemqOxJEt0X+SVAYBKTxObj1dHiIjoQk3V9415f9eC/KvE5rMUeM4/SJU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UxhQnuY+tKbOBOxPKWADsbbjfFot55hgrMZ14JBNm7b/U4XNH4tPofDKqGm25JW+QP/XgikcaNMduMKI20s+ypEwFV/cCnDtMkceWA3Ky8uM/T3EzG0IKwHGxZVxce3jMJtji2/GccPglalAAkvkH5jTjMOygOfqlWerLmiqTBTCu4DmjWv4hCUgY25dkxfzjXNrVLUaMFFJaS4TJWP2gQbX93Q3tt1xplkpYVY227fEY2f5fyxzLCkqi6NOq291Yas0PL8rKlZr89pdJZbChT4bE1L0hxW+nWNJ0i9t/idha65g1BskAr077JxtphKlSQTcb9fhiGW0bHQm+rrbF44OF3K7/AETOSlVKiyzlPSmjJQBs84lBTbqL369ugxAyC25CrT7kdCFjkEL5h3CSDc/y+YxpzQdVLiX3/wCJtv8ABWJ3D8kuVM338F/9qcdKXbQk97Js+nplSW5LgumPfVot7JI63BtiTmXK7QjS67Q5LVRZjhsyQyL3CkgHVYBJIPlVbuQTvfBPkpluQxmBLqEup/JssWWLj9Cv1xF+z4kPZVzU24AtHgZZ0q3G0V4jb4gH5DBS02Q73FoxATPZVLZcIZRZst3BKCB7JFtzsT8PngmyNk5nN+c6BSZjfOivS08wBIuUBKioX+A7YF8vf4iuI/U5WrT2uHUAH42J+pwxeDi1J4jZfIUQeYTcH9xeOfyWuBQ1+gSMs5mqlFec5jsCU5HN/wBYIUU6h9L4L6NJK6E201ZxKH1KPY3UlG3+k4i8aUgcZMzbD/FE/MpBJ+uLakstoypEWlCUrU4bqA3PlHXDrdDvkgqkyGHiHfu2ieo3tiQUIDgUjmLPqR1+v8salHU+AdxfofjiWs2cAGwIP8caEEV1orFktqHv1f1x+tR3dBGkHbYqN8Tkk8u/e2NaVHUNz1whGvlFCbLX5fhtjMSZGxPxxmGM/9k=");
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

     

     

    //将前端所有的data:image/jpeg;base64,/9jxxxxxxxx解析
    public static void base64UpLoad(String base64Data) throws Exception{
    String dataPrix = "";
    String data = "";

    if(base64Data == null || "".equals(base64Data)){
    throw new Exception("上传失败,上传图片数据为空");
    }else{
    String [] d = base64Data.split("base64,");
    if(d != null && d.length == 2){
    dataPrix = d[0];
    data = d[1];
    }else{
    throw new Exception("上传失败,数据不合法");
    }
    }
    // FileDtl fileDtl= new FileDtl();
    String suffix = "";
    if("data:image/jpeg;".equalsIgnoreCase(dataPrix)){//data:image/jpeg;base64,base64编码的jpeg图片数据
    suffix = ".jpg";
    } else if("data:image/x-icon;".equalsIgnoreCase(dataPrix)){//data:image/x-icon;base64,base64编码的icon图片数据
    suffix = ".ico";
    } else if("data:image/gif;".equalsIgnoreCase(dataPrix)){//data:image/gif;base64,base64编码的gif图片数据
    suffix = ".gif";
    } else if("data:image/png;".equalsIgnoreCase(dataPrix)){//data:image/png;base64,base64编码的png图片数据
    suffix = ".png";
    }else{
    throw new Exception("上传图片格式不合法");
    }
    String tempFileName = DateUtil.timeStampMS() + suffix;
    try {
    byte[] bs = Base64.decodeBase64(data);
    //处理数据
    for (int i = 0; i < bs.length; ++i) {
    if (bs[i] < 0) {
    bs[i] += 256;
    }
    }
    OutputStream out = new FileOutputStream("d:\\"+tempFileName);
    out.write(bs);
    out.flush();
    out.close();

    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    // fileDtl.setFileName(tempFileName);
    // // byte[] bs = Base64Utils.decodeFromString(data);
    // BASE64Decoder decoder = new BASE64Decoder();
    // byte[] bs = decoder.decodeBuffer(data);
    // //处理数据
    // for (int i = 0; i < bs.length; ++i) {
    // if (bs[i] < 0) {
    // bs[i] += 256;
    // }
    // }
    // int size=bs.length;
    // fileDtl.setFileSize((long)size) ;
    // InputStream is = new ByteArrayInputStream(bs);
    // fileDtl.setIs(is);
    // return fileDtl;
    }

    转载于:https://www.cnblogs.com/wth21-1314/p/10022174.html

    展开全文
  • Java 从网络中读取图片 转换Base64字符串
  • java生成二维码_java将二维码转换base64
  • 一、Java转换图片为base64 private static void ImageToBase64(String imgPath) { byte[] data = null; try (InputStream in = new FileInputStream(imgPath)){ data = new byte[in.available()]; ...

    一、Java转换图片为base64

    private static void ImageToBase64(String imgPath) {
        byte[] data = null;
        try (InputStream in = new FileInputStream(imgPath)){
            data = new byte[in.available()];
            in.read(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
       // 返回Base64编码过的字节数组字符串
       System.out.println("本地图片转换Base64: ");
       System.out.println(Base64.getEncoder().encodeToString(data));
    }
    

    二、把输出结果放入前端<img>标签中

    <img src="data:image/png;base64,java代码执行结果放在这里" />
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPQAAABzCAYAAACvmi+eAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAoxSURBVHhe7Z1fbxtZGcb7ObhaJD7J3vMVUkVcgIS42ssFShIKBQFa1AokJLpt76pU2m5J1GilqIKqvWmVKN2KigVV8YYNTU2aBCdpXDt5Oc+cczz/jj12xmOfnnl+0qPOnDkz9ljnN++ZcRVf6HQ6cnh4KM1mU1ZXV4UQ4j9wFc7CXTh8enoqZ2dncqHdbkur1ZLt7W25f/++6U4I8Rm4CmfhLhxOCX1wcCBbW1uytLRkuhNCfAauwlm4C4e73a4W+uTkRPb29qTRaMi9e/dMd0KIz8BVOAt34TCFJuQ9hkITEhAUmpCAoNCEBASFJiQgKDQhU+KTq3+QX/76d3J0dGRaYtCGbb+/9kfTMhwUmpAp8Ytf/VYufu8H8pOf/TwlNZbRhm3oMwoUmpApAXF/fGkhJXVSZmxLij4MFJqQKZKVuozMgEITMmWSUpeRGVBoQqYM5LWV2VZqCk3Ie0hSZlTm5PT7PFJTaEKmRFZmrCNlpKbQhEwJ+7VV9p45KTW+ix4FCk3IlMB/LIHUriqMNmxDn1GYntCv78jMt78jH1y8I2uLs/IBllVm5hd0e5RZWXwddZbFi3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nuAZ5qTgrdLd3b9xQ029N/L3ziqrMeXHDEDo9TdbVt2+ykpqpd/L+2xJV+ylKTaE9xTXI65QixjFwQ4RCe4prkNcpRVBoNxTaU1yDvE4pgkK7odCe4hrkdUoRFNoNhfYU1yCvU4qg0G4otKe4BnmdUgSFdkOhPYV/aH8w/EP7efiH9j2GP4UzGP4UTh7+FI7H8MfqBhP9WN3WdjSAUZnqDM4fnwM+D8hXFgpdEfw52cF0OubnZNU0E/eOdQ3OP/o5WfV5jAMKTUhAUGhCAoJCExIQFJqQgKDQhAQEhSYkICg0IQFBoQkJCApNSEBQaEICgkITEhAUmpCAoNCEBASFJiQghhJ6eXlZ9ven+6sChJDBwFG46hS63W7LwcGBbG1tyYMHD+TRo0fy5s0bsyshxCfgJhyFq3AW7sLhlNCtVku2t7fl+fPnsrKyIrdv35Zbt27JjRs3GIbxJHASbsJRuApn4S4cPj091UJ3Oh05PDyUZrMpL1++lGfPnsmTJ0/k8ePH0ZXg4cOHDMNMOXARTsJNOApX4SzchcM9oVGqMQdH6X716lXU8cWLF9EVYGNjg2EYTwIn4SYchatw1t4/94TGAgw/Pj6OOsB6lHLMz3HTzTCMH4GTcBOOwlU4a6tzSmgYjg2w/ejoKJqXYwc8QWMYxo/ASbgJR+EqnE1WZ3ABC9ZwKzZushHsxDCMH7FeJkW2MqeEtrEdrNwMw/iVpKNJdzUi/wdmMjwOfSc92QAAAABJRU5ErkJggg=="/>
    </body>
    </html>
    

    三、效果

    展开全文
  • java实现将图片转为base64格式

    千次阅读 2019-06-14 15:54:25
    logoImgUrl = getImgStr(url);//图片链接 public static String getImgStr(String imgFile) { ByteArrayOutputStream data = new ByteArrayOutputStream(); try { // 创建URL ...
  • 主要介绍了Java实现文件和base64流的相互转换功能,涉及Java文件读取及base64 转换相关操作技巧,需要的朋友可以参考下
  • java 将图片转换Base64编码的字符串

    千次阅读 2018-11-21 10:43:18
    * 将图片转换Base64编码的字符串 * @param path * @return base64编码的字符串 */ public static String imageToBase64(String path){ if(TextUtils.isEmpty(path)){ return null; } InputStream is = nu....
  • java将图片转成Base64编码

    热门讨论 2012-09-26 14:28:45
    一款转码示例,可以直接运行。java将图片转成Base64编码。
  • 将图片文件转化字节数组字符串,并对其进行Base64编码处理 /** * @Descriptionmap 将图片文件转化字节数组字符串,并对其进行Base64编码处理 * @author temdy * @Date 2015-01-26 * @param path 图片路径 ...
  • 主要介绍了Java处理图片实现base64编码转换,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
  • 图片url转base64位,base64位的数据转换图片,附带工具类
  • 代码并不是想象中那么复杂够平时进行简单的转换使用了,真实项目中建议进一步优化。
  • 图片base64编码互相转换的一个工具类
  • 将图片服务器地址转化为BASE64位字符串。 import org.apache.commons.codec.binary.Base64; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net....
  • java 将图片转为base64返回给前端

    千次阅读 2020-02-13 14:07:24
    base64 = base64.replaceAll("\n", "").replaceAll("\r", ""); object.put("code", "data:image/jpg;base64," + base64); response.getWriter().write(object.toString()); } catch (IOException e) { ...
  • js html java servlet 前台接收base64数据后台转图片保存上传 全套代码 直接可以用
  • String s = encodeImgageToBase64("http://172.16.170.54:9332/13,55bb6bae8203"); List<Map<String,String>> list = new ArrayList<>(); Map<String,String> map = new HashMa.
  • 1、实现 MultipartFile 接口 package com.file.demo.base64ToMultipartFile; import org.springframework.web.multipart.... * 自定义的MultipartFile的实现类,主要用于base64上传文件,以下方法都可以根据...
  • 一个cordova插件,提供图像转换为base64的功能。 安装插件 cordova plugin add https://github.com/autobotsrocks/cordova-imageBase64 用法 转换位置图片 window . autobots . imageBase64 . base64 ( '/...
  • java 压缩图片 图片转换为base64

    千次阅读 2019-07-25 13:15:17
    * 根据指定大小压缩图片 * * @param imagePath 源图片地址(网络地址) * @param desFileSize 指定图片大小,单位kb * @param imageId 影像编号 * @return 压缩质量后的图片字节数组 */ public static ...
  • Java中将base64编码字符串转换为图片

    千次阅读 2020-03-24 10:25:11
    一段时间,在做摄像头拍照上传,摄像头拍的照片为base...1、将base64编码字符串转换为图片的代码如下 ImageUtil.java: package util; import javax.servlet.http.HttpServletRequest; import java.io.FileOutputSt...
  • JAVA 文件转换为Base64字符串(互转)

    千次阅读 2019-11-24 17:12:47
    数据库:先文件上传的文件通过Base64转换为字符串,然后对应Base64字符串存储到数据库。 数据库获取---->到页面:从数据库获取对应Base64字符串,通过Base64转换为图片文件,输出到页面。 实现: 文件和...
  • InputStream in = null; ...//读取图片字节数组 try { in = new FileInputStream(你的文件路径); //该路径只能是本机路径,不能是网络地址 data = new byte[in.available()]; in.read(...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 30,449
精华内容 12,179
关键字:

java将图片转换为base64

java 订阅