2、controller
@Inject
private IUserService userService;
@RequestMapping(value="test")
@ResponseBody
public ConsoleResult test(String base64){
// 自定义返回前台数据格式
ConsoleResult res = new ConsoleResult();
// 去掉base64数据头部data:image/png;base64,和尾部的” " “
String[] ww= base64.split(",");
base64 = ww[1];
String[] aa = base64.split("\"");
base64 = aa[0];
try {
// 将图片插入数据库
userService.base64test(base64);
// 图片保存到本地
String path = "D:/asdfasdf.jpg";
Base64File file = new Base64File();
file.decoderBase64File(base64, path);
// 成功标识
res.setStatus(ConsoleResult.successStatus);
} catch (Exception e) {
res.setStatus(ConsoleResult.faultStatus);
}
return res;
}
3、base64
/**
* 将base64字符解码保存文件
*
* @param base64Code
* @param targetPath
* @throws Exception
*/
public static void decoderBase64File(String base64Code, String targetPath) {
byte[] buffer;
FileOutputStream out = null;
try {
buffer = new BASE64Decoder().decodeBuffer(base64Code);
out = new FileOutputStream(targetPath);
out.write(buffer);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
4、mapper.xml
update t_user set U_ABOUT = #{base64} where u_name = '971171444'
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。