解码Base64,并保存图片至本地
五一去了具有诗情画意的城市---杭州,今天是假期结束后回来上班的第一天,玩饱之后回来,确实精神抖擞了不少;
前段时间开发了有关电子签名的需求,其中有个关于解码Base64图片的知识点,值得关注一下;
情况是这样的:
我在签字板上 , 签完名并点击确定之后,生成的图片路径是base64格式的,领导想将签名生成的图片保存在本地,那么问题的重点来了,怎么样将base64格式的图片保存至本地;
下面一朵代码是我的解决方案:
@Test
public void base64(){
String imgStr = "data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAABRCAYAAACXMekVAAASFElEQVR4Xu3dBbDsynEG4N9hZmZmZnaYmdlhZqpgJXbYYWZmZmaww8yJw8zMWN+rmdSU3oKklXa1e6arbt17z5FGM635p7l1n3TqHOgc2MuB+3TedA50DuznQAdI3x2dAwc40AHSt0fnQAdI3wMrcuDhk/jzDys+42JDdwlyMdbfzIO/PcljJXlgkq+/mVWVhXSA3NobPe96XivJ15ZHAof/3xR1gNzU6zz7Yl4xybeVp/55kic4+wxWfmAHyMoMvgPD/1mSxy/rBJjvuKU1d4Dc0tu8zFq+N8lLlUd/ZJL3v8w01nlqB8g6fL1Lo356krcvC/7HJE+a5O9vhQEdILfyJi+3jldIwpNV6UWSPOhy01n2yR0gy/LzLo72cEm+KslzJ/m1JK+S5D9uhREdILfyJi+7jkdJ8sRJfveWwIGlHSC7N9bDJnmEJP+c5H8uu/f60y/JgQ6Qe3P/MZN8VpInS8IA/eJLvqD+7MtyoAPk3vx/pySfUn78E0leJgnvTKc7yIE5AHHPsyV5tCQPTvLfN8a3ZyjGpmX9e5L7JgGUTneQA3MA8rRJfjLJIyV51ySfuSDfzIfBxwtic16K2uDXDyd56ST/eanJ9OdejgNzAPKeST62TPmfkkgv+JEFlmAur5/k3Yo3xL8vRfdL8jlJGOvobZN89qUmc0XPfc4kf5pE+slN0ByAyLv56SRPUjjw60mef2Y9wEMVSfQ4Sd47yVuW2gJDv3mSL7wQlx+3qI9PU57/K0leNMnfXmg+1/DYD0ryg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fIUpzs49wkB/4P6ctJf+JxZ3EAAAAASUVORK5CYII=";
String imgFilePath = "D:/youarebeautiful";
String imgFileName = "/20170503.jpg"; File file = new File(imgFilePath); if(!file.exists()){//文件根目录不存在时创建
new File(imgFilePath).mkdirs();
} System.out.println("图片完整路径:"+imgStr);
System.out.println("截取图片路径base64部分内容:"+imgStr.substring(22)); // 图像数据为空
if (imgStr == null){
System.out.println("imgStr is null");
}
BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64解码
byte[] bytes = decoder.decodeBuffer(imgStr.substring(22)); System.out.println("bytes的长度:"+bytes.length); for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {// 调整异常数据
bytes[i] += 256;
}
}
// 生成jpg图片
OutputStream out = new FileOutputStream(imgFilePath+imgFileName);
out.write(bytes);
out.flush();
out.close();
} catch (Exception e) { }
}
复制代码即可一试 , 拿走 , 不谢~
解码Base64,并保存图片至本地的更多相关文章
- HTML 保存图片到本地
具体方法有两种 一种是 利用canvas的 toDataUrl 和Html5 里面的 <a>标签里面的 Download 属性 虽然 Download 的兼容性不怎么样 但是在文章后 ...
- 原生js 保存图片到本地
<template> <div> <!--创建一个cavas 用来存放图片--> <canvas id="cavasimg" width= ...
- JS/HTML 保存图片到本地:HTML <a> download 属性
JS如何保存图片到本地呢?自己百度一下吧! 这里想要说的是,可以利用 HTML 的 <a> 标签 来是实现保存图片到本地的功能,参考代码如下: <a href="http: ...
- Python解码base64遇到Incorrect padding错误
Python解码base64遇到Incorrect padding错误 base64转码过程 先说一下转换过程,详细的可以参考阮一峰.廖雪峰博客: 所谓Base64,就是说选出64个字符----小写字 ...
- js+Canvas 利用js 实现浏览器保存图片到本地
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Android笔记之使用ImageView加载网络图片以及保存图片到本地并更新图库
ImageView显示网络图片 findViewById(R.id.btnLoad).setOnClickListener(new View.OnClickListener() { @Override ...
- Java,获取文件的Base64字符串,解码Base64字符串还原文件
在jdk1.8以前,获取文件Base64字符串需要用到第三方库,从1.8开始,Java中引入了Base64相关的类 以下是代码示例 获取文件的Base64编码字符串 import java.io.Fi ...
- 微信小程序点击保存图片到本地相册——踩坑
在微信小程序中要保存图片到本地相册,需要获取相册权限. 总之整个功能实现下来需要如下几个小程序的API:wx.getSetting,wx.authorize,wx.openSetting,wx.dow ...
- JAVA将图片(本地或者网络资源)转为Base64字符串,将base64字符串存储为本地图片
代码如下: package com.futuredata.dataservice.util; import java.io.ByteArrayOutputStream; import java.io. ...
随机推荐
- Kotlin语言学习笔记(4)
函数 // 函数定义及调用 fun double(x: Int): Int { return 2*x } val result = double(2) // 调用方法 Sample().foo() / ...
- JDK、JRE和JAR区别(转载)
JDK里面的工具也是用Java编写的,它们本身运行的时候也需要一套JRE,如C:/Program Files/Java/jdk1.5.x/目录下的JRE.而C:/Program Files/Java/ ...
- springmvc处理url请求步骤
处理步骤: 1.调用DispatcherServlet的doService方法 2.进入DispatchServlet的doDispatch方法 3.进入DispatchServlet的getHand ...
- ValueError: update only works with $ operators
问题:在执行pymongo的update语句时,提示了ValueError: update only works with $ operators 脚本:db.user.update_one({&qu ...
- Cookie的Domain属性
Cookie 加了Domain后就写不进去了(不加domain就可以写进去了) 本地测试的时候需要把domain换成localhost cookie跨域的问题,意思就是说A.com下能访问B.com域 ...
- ios web input 内边阴影
ios网页中,默认input上部有阴影,去除方法: -webkit-appearance: none;
- 安装tftp服务器进行文件传输
1. 安装: sudo apt-get install tftp-hpa tftpd-hpa ps: tftpd是服务器,tftp是客户端,客户端能发送和获取,服务器不能动. 2. 配置文件: sud ...
- 【c++】多层次继承类对象的构造函数参数的传递方法
#include <iostream.h> //基类CBase class CBase { int a; public: CBase(int na) { a=na; cout<< ...
- 121. Best Time to Buy and Sell Stock买卖股票12
一 [抄题]: If you were only permitted to complete at most one transaction (ie, buy one and sell one sha ...
- Golang之redis
redis是个开源的高性能的key-value的内存数据库,可以把它当成远程的数据结构. 支持的value类型非常多,比如string.list(链表).set(集合). hash表等等 redis性 ...