image与base64相互转换
image与base64相互转换
数据库储存图片时,可以将图片转换为base64进行存储,数据库用text进行存储。但是使用eclipse实现,可能会出现无法使用BASE64Encoder。
解决方法:项目名右键->Build Path->configure bild path->Libraryies->JRE System Library->acess rules->edit->add->resolution:acessible Rule Pattern:**
image转base64
public static String ImageToBase64ByPath(String imgPath) {InputStream in = null;byte[] data = null;try {in = new FileInputStream(imgPath);data = new byte[in.available()];in.read(data);in.close();} catch (IOException e) {e.printStackTrace();}BASE64Encoder encoder = new BASE64Encoder();return encoder.encode(data);}
URL转base64
public static String ImageToBase64ByUrl(String imgURL) {ByteArrayOutputStream data = new ByteArrayOutputStream();try {URL url = new URL(imgURL);byte[] by = new byte[1024];HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(5000);InputStream is = conn.getInputStream();int len = -1;while ((len = is.read(by)) != -1) {data.write(by, 0, len);}is.close();} catch (IOException e) {e.printStackTrace();} BASE64Encoder encoder = new BASE64Encoder();return encoder.encode(data.toByteArray());}
base64转image
public static boolean Base64ToImage(String imgStr, String imgFilePath) {if ("".equals(imgStr) || imgStr == null) return false;BASE64Decoder decoder = new BASE64Decoder();try {byte[] b = decoder.decodeBuffer(imgStr);for (int i = 0; i < b.length; ++i) {if (b[i] < 0) {b[i] += 256;}}OutputStream out = new FileOutputStream(imgFilePath);out.write(b);out.flush();out.close();return true;} catch (Exception e) {return false;}}
image与base64相互转换相关推荐
- base64 string 放不下_String、byte[]、Base64相互转换,不要再用错了
String与byte[] 在Java项目开发过程中,时常会遇到String与byte[]互相转换的过程,比如IO流处理,需要先将文件或字符串转为字节流,接收方需要将字节流转回字符串.那么,在相互转换 ...
- 字符串与base64相互转换
字符串转base64 function encode(str){// 对字符串进行编码var encode = encodeURI(str);// 对编码的字符串转化base64var base64 ...
- URL Safe base64 与 base64相互转换
为什么需要base64? ASCII码一共规定了128个字符的编码,这128个符号,范围在[0,127]之间. 其中,[0,31],及127, 33个属于不可打印的控制字符. 在电子邮件传输信息时,有 ...
- 将图片转换为base64_图片与base64相互转换
图片转base64 <input id="file" accept="image/png,image/jpeg,image/gif" type=" ...
- python 图片与base64相互转换
import base64 import io from PIL import Image# 图片转换成base64 def image_to_base64(path):with open(path, ...
- pdf与base64相互转换的工具类
import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder;import javax.servlet.ServletOutputStrea ...
- 如何将 image 转成 base64 字符串?
咨询区 vaj90 我的项目中有一个需求,需要将 base64 格式的字符串 data:image/gif;base64,/9j/4AAQSkZJRgABAgEAYABgAAD.. 转成 image ...
- 【微信小程序】图片选择、转码、压缩、预览、上传,file与base64
0 前言 微信小程序中,上传图片,如标题,涉及图片选择.图片转码.图片压缩.图片预览.图片上传等 1 图片选择 需要使用微信提供的wx.chooseMedia chooseImage() {var t ...
- 密码学-编码算法:Base64编码原理和使用
1.Base64简介 Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法.Base64编码是从二进制到字符的过程,可用于在 ...
最新文章
- MySQL—交叉连接、自然连接、内连接
- redis常用API
- 今晚直播 | 清华大学NLP组秦禹嘉:基于自然语言解释的数据增强
- boost::gil模块临界点threshold的测试程序
- 北大计算机科学与技术专业河南录取分数线,北京大学河南录取分数线|2021北京大学河南分数线|2021年北京大学河南高考分数线|2021年北京大学河南招生计划录取人数...
- 各种数据库获取前10行记录实例
- Jmock-使用ClassImposteriser扩展mock class对象
- 在碎片化阅读充斥眼球的时代,要高效读论文
- python 博弈论 社会网络_【 统计应用 】 社会网络分析SNA报告-学习
- 电商夏季促销海报设计PSD模板,分解教你如何设计
- Enterprise Library 4.0 - May 2008 发布了
- 基本分段存储管理方式
- JAVA版本8u171与8u172的区别
- 2021毛概知识点章节整理(完整版)
- 矩形连乘问题c语言,矩阵连乘问题
- kurento服务器搭建(docker方式)
- 数据模型(LP32 ILP32 LP64 LLP64 ILP64 )
- 论文封面摘要目录不设置页码-word2013
- C/C++笔试必须熟悉掌握的头文件系列(九)——string
- 西门子 延时 光控 声控开关 5TG0 211 -1CC1 提高 灵敏度 的方法