1问题
在使用base64进行图片上传时。string字符串会经过http协议进行转义,所以要将转义后的字符串转回原始字符串再进行解析
转义后的字符:
data%3Aimage%2Fjpeg%3Bbase64%2C%2F9j%2F4AAQSkZJRgABAQAAAQABAAD%2F2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB%0AAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH%2F2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB%0AAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH%2FwAARCAHgAeADASIA%0AAhEBAxEB%2F8QAFQABAQAAAAAAAAAAAAAAAAAAAAn%2FxAAUEAEAAAAAAAAAAAAAAAAAAAAA%2F8QAFQEB%0AAQAAAAAAAAAAAAAAAAAAAAn%2FxAAUEQEAAAAAAAAAAAAAAAAAAAAA%2F9oADAMBAAIRAxEAPwCO4CS6%0AG4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA%0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA%0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA%0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA%0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA%0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA%0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/9k=
base64转换地址:
http://base64.xpcha.com/pic.html
图片
参数:
后台接收代码:
@RequestMapping(value = "suggestion", method = {RequestMethod.POST})
public ResultData addSuggestion(
@RequestBody String data)
{
List<String> urls = new LinkedList<String>();
//文件的保存路径
String savePath = saveurl.replace("//", "\\")+fileurl.replace("/", "\\")+"\\";
try {
//File filedir = new File("D:\\gwxStatic\\static");
//如果文件夹不存在,则创建文件夹
File filedir = new File(saveurl.replace("//", "\\\\")+fileurl.replace("/", "\\\\"));
if(!filedir.exists()){
filedir.mkdirs();
}
JSONObject para = JSONObject.parseObject(data);
JSONObject jb = para.getJSONObject("data");
JSONObject jc = para.getJSONObject("certification");
JSONArray base64StringArray = jb.getJSONArray("files");
//拿到base64的字符串数组
String[] array = base64StringArray.toString().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(savePath+urls.get(i));
}
String code = UUID.randomUUID().toString();
log.error(code, e.getCause());
return new ResultData(false, "500", "后台程序异常 logId" + code);
}
}
public boolean deleteFile(String fileName) {
File file = new File(fileName);
// 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
if (file.exists() && file.isFile()) {
if (file.delete()) {
log.error("删除单个文件" + fileName + "成功!");
return true;
} else {
log.error("删除单个文件" + fileName + "失败!");
return false;
}
} else {
log.error("删除单个文件失败:" + fileName + "不存在!");
return false;
}
}
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();
log.error(code, e);
log.error("保存图片失败");
}
}
注释很详细了,基本都可以看懂