base642photo
/**
* pic to base64Str
* @param path 读取路径
* @return
*/
public static String GetImageStr(String path) {//转化成base64字符串
String imgFile = path;
InputStream in = null;
byte[] data = null;
try {
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);// 返回Base64编码过的字节数组字符串
} catch (IOException e) {
logger.error("图片转换base64字符串出错"+e.getMessage());
}
return "";
}
/**
* base64Str to pic
* @param imgStr base64 字符串
* @param path 存储路径
* @return
*/
public static boolean GenerateImage(String imgStr,String path) { // 对字节数组字符串进行Base64解码并生成
if (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;
}
}
File file = new File(path);
if (!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
OutputStream out = new FileOutputStream(new File(path));
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
base642photo的更多相关文章
随机推荐
- 2018.2.09 php学习(二)
1.用索引提高效率: 索引是表的一个概念部分,用来提高检索数据的效率,ORACLE使用了一个复杂的自平衡B-tree结构. 通常,通过索引查询数据比全表扫描要快. 当ORACLE找出执行查询和Upda ...
- OpenCascade:Topo类型转换
OpenCascade:Topo类型转换 TopoDS_Edge newEdge; if (oldShape.ShapeType()==TopAbs_EDGE) newEdge=TopoDS::Edg ...
- 虚IP切换原理
高可用性HA(High Availability)指的是通过尽量缩短因日常维护操作(计划)和突发的系统崩溃(非计划)所导致的停机时间,以提高系统和应用的可用性.HA系统是目前企业防止核心计算机系统因故 ...
- Google 出品的 Java 编码规范,强烈推荐,权威又科学!
原文:google.github.io/styleguide/javaguide.html 译者:Hawstein 来源:hawstein.com/2014/01/20/google-java-sty ...
- Java写诗程序
import java.util.Random; public class test_word { public static void main(String[] args) { System.ou ...
- c++ 定义一个结构体student,输入多个student的信息并以三种方式显示
#include <iostream> #include <string> using namespace std; const int slen = 30; struct s ...
- pandas处理大文本数据
当数据文件是百万级数据时,设置chunksize来分批次处理数据 案例:美国总统竞选时的数据分析 读取数据 import numpy as np import pandas as pdfrom pan ...
- jenkins 全局工具配置
- Mybatis 循环 foreach, 批量操作
mapper.java: int modifySortOfGoods(@Param("idlist") List<String> goodsIds, @Param(&q ...
- 【laravel】laravel class 里面定义以head开头的方法会报错
BadMethodCallException in Macroable.php line 81:Method head does not exist.