关于import sun.misc.BASE64Decoder; base64文件通过Ajax传值时出现的乱码问题解决,以及实现html5调用windows摄像头保存在本地
乱码问题
如果你只是出现了ajax替换了base64值问题的话,如果使用的是sun公司的jar,代码如下:
sj是我的传到后台的base64的字符串
//替换所有的%2B为+
sj=sj.replaceAll("%2B","+");
//替换所有的%2F为/
sj=sj.replaceAll("%2F","/");
//替换所有的%3D
sj=sj.replaceAll("%3D","=");
//找到头的位置并且去掉 千万注意去掉你的头不要有多余字符!!!!
int aa=sj.indexOf("=");
sj=sj.substring(aa+1);
后台部分代码(使用springmvc方式)
public @ResponseBody Map<String,Object> test2(@RequestBody String sj) throws Exception {
sj=sj.replaceAll("%2B","+");
sj=sj.replaceAll("%2F","/");
sj=sj.replaceAll("%3D","=");
int aa=sj.indexOf("=");
sj=sj.substring(aa+1);
GenerateImage(sj);
//下面为人工智能识别图片代码没有关系可以删掉的,return map就ok了
FaceMatch faceMatch=new FaceMatch();
Map<String,Object> map=new HashMap<>();
int score=0;
int indexScore=0;
String resultMsg=faceMatch.match("F:\\1.png","F:\\3.png");
map.put("msg","失败");
indexScore=resultMsg.indexOf("\"score\"");
score=Integer.parseInt(resultMsg.substring(indexScore+8,indexScore+10));
System.out.println(score);
if (score>=80)
map.put("msg","成功");
else
map.put("msg","失败");
return map;
}
// base64字符串转化成图片
public boolean GenerateImage(String imgStr) throws Exception{ // 对字节数组字符串进行Base64解码并生成图片
if (imgStr == null) // 图像数据为空
return false;
BASE64Decoder decoder = new BASE64Decoder();
byte[] b = decoder.decodeBuffer(imgStr);
// Base64解码,对字节数组字符串进行Base64解码并生成图片
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
// 生成jpeg图片
// String imgFilePath = "c://temp_kjbl_001_ab_010.jpg";//新生成的图片
OutputStream out = new FileOutputStream("F:\\1.png");
out.write(b);
out.flush();
out.close();
return true;
}
前端代码如下:
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
<body>