java后台–base64编码格式变成图片
当图片转成了base64格式后,传到后台对base64解码并生成图片
如一下代码所示。
/**
* @Description: base64字符串转化成图片
* @param: imgStr
* @Return:
*/
@RequestMapping(value = "/uploads",method=RequestMethod.POST,produces = { "application/json;charset=UTF-8" })
@ResponseBody
public String uploads(String imgStr,HttpServletRequest req,HttpServletResponse respon,MultipartFile myfile)
{
//对字节数组字符串进行Base64解码并生成图片
//图像数据为空
if (imgStr == null)
return "error";
BASE64Decoder decoder = new BASE64Decoder();
try
{
//Base64解码
byte[] b = decoder.decodeBuffer(imgStr);
for(int i=0;i<b.length;++i)
{
if(b[i]<0)
{
//调整异常数据z
b[i]+=256;
}
}
//生成jpeg图片
// String imagePath = req.getSession().getServletContext().getRealPath("/");
String imagePath = "D:/搜狗高速下载";
//新生成的图片
String imgFilePath = imagePath+"1122333";
System.out.print(imgFilePath);
OutputStream out = new FileOutputStream(imgFilePath);
System.out.print(out);
out.write(b);
out.flush();
out.close();
return out.toString();
}
catch (Exception e)
{
return "error";
}
}