//方案1 纯前端实现
/*********** START ****** 纯前端实现 Base64图片互转换********** START ******/
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width" />
<title>jquery 图片base64</title>
<script src="./jquery-1.11.3.min.js"></script><!-- 引用jquery -->
</head>
<body>
<input id="testFile" type="file">
<hr>
<img id="testImg" style="max-height: 300px; height: 8em; min-width:8em;">
<hr>
<textarea id="testArea" style="display: block; width: 100%;height: 30em;"></textarea>
<input id="btnTest" type="button" value="提交base" />
</body>
<script>
$("#testPhone").click(function () {
$("#testFile").click();
});
$("#testFile").change(function () {
run(this, function (data) {
$('#testImg').attr('src', data);
$('#testArea').val(data);
});
});
$("#btnTest").click(function () {
$.ajax({
url: "/usercenter/testbaseaction",
type: "post",
dataType: "json",
data: {
"content": $("#testArea").val(),
},
async: false,
success: function (result) {
if (result.Code == 200) {
alert(result.Data);
} else {
}
}
});
});
function run(input_file, get_data) {
/*input_file:文件按钮对象*/
/*get_data: 转换成功后执行的方法*/
if (typeof (FileReader) === 'undefined') {
alert("抱歉,你的浏览器不支持 FileReader,不能将图片转换为Base64,请使用现代浏览器操作!");
} else {
try {
/*图片转Base64 核心代码*/
var file = input_file.files[0];
//这里我们判断下类型如果不是图片就返回 去掉就可以上传任意文件
if (!/image\/\w+/.test(file.type)) {
alert("请确保文件为图像类型");
return false;
}
var reader = new FileReader();
reader.onload = function () {
get_data(this.result);
}
reader.readAsDataURL(file);
} catch (e) {
alert('图片转Base64出错啦!' + e.toString())
}
}
}
</script>
</html>
/******** END ****** END ******纯前端 Base64图片互转换******** END ******* END ******/
//方案2 纯后端实现
/******START*****START***纯后端口实现 JAVA Base64图片互转换******START*****START***/
/**
* @Title: GetImageStrFromPath
* @Description: TODO(将一张本地图片转化成Base64字符串)
* @param imgPath 图片路径
* @return
*/
public static String GetImageStrFromPath(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();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
// 返回Base64编码过的字节数组字符串
return encoder.encode(data);
}
/**
* @Title: GenerateImage
* @Description: TODO(base64字符串转化成图片)
* @param imgStr 图片base64字符串
* @param imgFilePath 存图片的路径
* @return
*/
public static boolean GenerateImage(String imgStr,String imgFilePath) {
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;
}
}
// 生成jpg图片
// String imgFilePath = "C://Users//LQ//Downloads//222.jpg";
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){
basePath="D:\\tmp\\6fc83a591689474295a9fae8790e956c\\img\\funnel_one.png";
String cc= GetImageStrFromPath(basePath);
System.out.println(cc);
GenerateImage(cc);
}
/****** END ***** END ***纯后端口实现 JAVA Base64图片互转换****** END ***** END ***/