Java后台接收二进制流和Base64数据保存图片
Java后台用的是Springmvc框架,接收前台传过来的二进制流数据或者是base64的字符串图片数据。
接收二进制流数据:
@RequestMapping("/uploadImg")
@ResponseBody
public Object uploadImg(HttpServletRequest request, HttpServletResponse response) throws IOException {
System.out.println("图片上传开始...");
String destDir = "/upload/image";
/* response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST");*/
ServletInputStream inputStream = request.getInputStream();
//获取文件上传的真实路径
String uploadPath = request.getSession().getServletContext().getRealPath("/");
//保存文件的路径
String filepath = destDir + File.separator + createNewDir();
File destfile = new File(uploadPath + filepath);
if (!destfile.exists()) {
destfile.mkdirs();
}
//文件新名称
String fileNameNew = getFileNameNew() + ".png";
File f = new File(destfile.getAbsoluteFile() + File.separator + fileNameNew);
if (!f.exists()) {
OutputStream os = new FileOutputStream(f);
BufferedOutputStream bos = new BufferedOutputStream(os);
byte[] buf = new byte[1024];
int length;
length = inputStream.read(buf, 0, buf.length);
while (length != -1) {
bos.write(buf, 0, length);
length = inputStream.read(buf);
}
bos.close();
os.close();
inputStream.close();
String lastpath = filepath + File.separator + fileNameNew;
System.out.println("返回图片路径:" + lastpath);
return lastpath;
}
return false;
}
接收base64的字符串数据:
/**
* 将接收的base64转换成图片保存
*
* @param imgByte
* base64数据
* @param cardNum
* 号码
* @return 成功返回图片保存路径,失败返回false
*/
@RequestMapping("/saveToImgByStr")
@ResponseBody
public Object saveToImgByStr(String imgByte,String cardNum,HttpServletRequest request,HttpServletResponse response) {
String destDir = "/upload/image";
/* response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST");*/
imgByte=imgByte.replaceAll("data:image/png;base64,","");
BASE64Decoder decoder = new BASE64Decoder();
byte[] imageByte = null;
try{
imageByte = decoder.decodeBuffer(imgByte);
for (int i = 0; i < imageByte.length; ++i) {
if (imageByte[i] < 0) {// 调整异常数据
imageByte[i] += 256;
}
}
} catch (Exception e) {
e.printStackTrace();
}
if (imageByte.length>0) {
try {
//获取文件上传的真实路径
String uploadPath = request.getSession().getServletContext().getRealPath("/");
//保存文件的路径
String filepath = destDir + File.separator + createNewDir();
File destfile = new File(uploadPath + filepath);
if (!destfile.exists()) {
destfile.mkdirs();
}
//文件新名称
String fileNameNew = getFileNameNew() + ".png";
File f = new File(destfile.getAbsoluteFile() + File.separator + fileNameNew);
// 将字符串转换成二进制,用于显示图片
// 将上面生成的图片格式字符串 imgStr,还原成图片显示
InputStream in = new ByteArrayInputStream(imageByte);
FileOutputStream fos = new FileOutputStream(f);
// BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] buf = new byte[1024];
int length;
length = in.read(buf, 0, buf.length);
while (length != -1) {
fos.write(buf,0,length);
length = in.read(buf);
}
fos.flush();
fos.close();
in.close();
String lastpath = filepath + File.separator + fileNameNew;
System.out.println("返回图片路径:" + lastpath);
return lastpath;
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
return false;
}
util方法:
/**
* 为文件重新命名,命名规则为当前系统时间毫秒数
*
* @return string
*/
private static String getFileNameNew() {
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
return fmt.format(new Date());
}
/**
* 以当前日期为名,创建新文件夹
*
* @return
*/
private static String createNewDir() {
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
return fmt.format(new Date());
}