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%0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/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("儲存圖檔失敗");
}
}
注釋很詳細了,基本都可以看懂