base64进⾏图⽚上传遇http转义问题总结
1问题
在使⽤base64进⾏图⽚上传时。string字符串会经过http协议进⾏转义,所以要将转义后的字符串转回原始字符串再进⾏解析
转义后的字符:
data%3Aimage%2Fjpeg%3Bbase64%2C%2F9j%2F4AAQSkZJRgABAQAAAQABAAD%2F2wBDAAEBAQEBAQEBAQEBAQEBAQ
原始字符:
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
base64转换地址:
图⽚
参数:
后台接收代码:
@RequestMapping(value = "suggestion", method = {RequestMethod.POST})    public ResultData addSuggestion(
@RequestBody String data)
{
List<String> urls = new LinkedList<String>();
//⽂件的保存路径
String savePath = place("//", "\\")+place("/", "\\")+"\\";
try {
//File filedir = new File("D:\\gwxStatic\\static");
//如果⽂件夹不存在,则创建⽂件夹
File filedir = new place("//", "\\\\")+place("/", "\\\\"));            if(!ists()){
filedir.mkdirs();
}
JSONObject para = JSONObject.parseObject(data);
JSONObject jb = JSONObject("data");
JSONObject jc = JSONObject("certification");
JSONArray base64StringArray =  jb.getJSONArray("files");
//拿到base64的字符串数组
String[] array = String().split(",");
//解析并保存⽂件
for (int i = 0; i < array.length; i++) {
//将经过http协议转换后的字符串转换为原始base64字符串
String basee64code = URLDecoder.decode(array[i], "UTF-8");
//解析字符串并保存成图⽚
String uuid = UUID.randomUUID().toString();
base64CodeToimage(basee64code.split(",")[1],savePath+uuid+".jpg");
urls.add(uuid+".jpg");
}
String tokenID = new String();
if (jc.containsKey("tokenID")) {// 验证tokenID存在且不为空
tokenID = jc.getString("tokenID");
if (StringUtil.isNullOrEmpty(tokenID)) {
return new ResultData(false, "200", " tokenID 不能为空");
}
// 验证data
if (jb.containsKey("suggestionType")
&& jb.containsKey("suggestionContent")) {
String suggestionType = jb.getString("suggestionType");
if (StringUtil.isNullOrEmpty(suggestionType)) {
return new ResultData(false, "400", "反馈类型必填");
}
String content = jb.getString("suggestionContent");
return settingService.addSuggestion(tokenID,
suggestionType, content, urls);
} else {
return new ResultData(false, "200",
"关键字suggestionType或suggestionContent错误");
}
} else {
return new ResultData(false, "400", "关键字 tokenID 错误");
}
} catch (Exception e) {
//e.printStackTrace();
// 保存失败,删除之前保存的⽂件
for (int i = 0; i < urls.size(); i++) {
deleteFile((i));
}
String code = UUID.randomUUID().toString();
<(code, e.getCause());
return new ResultData(false, "500", "后台程序异常  logId" + code);        }
}
/**
* 如果保存失败,删除之前保存的⽂件
*
* @Description
* @Param
* @Return
*/
public boolean deleteFile(String fileName) {
File file = new File(fileName);
/
/ 如果⽂件路径所对应的⽂件存在,并且是⼀个⽂件,则直接删除
if (ists() && file.isFile()) {
if (file.delete()) {
<("删除单个⽂件" + fileName + "成功!");
return true;
} else {
<("删除单个⽂件" + fileName + "失败!");
return false;
}
} else {
<("删除单个⽂件失败:" + fileName + "不存在!");
return false;
}
}
/**
* base64字符串转换为图⽚,并保存
* @Description
* @Param
* @Return
*/
public static void base64CodeToimage(String basee64code,String url) {
BASE64Decoder bd = new BASE64Decoder();
try {
File file = new File(url);
FileOutputStream fo = new FileOutputStream(file);
fo.write(bd.decodeBuffer(basee64code));
fo.flush();
fo.close();
} catch (IOException e) {
String code = UUID.randomUUID().toString();
<(code, e);
<("保存图⽚失败");
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。