具体获取宽和高的方法:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3844e307c4f4b6d33e2a2afcae2ecf18.png)
前端传过来的是base64编码后的图片
具体代码如下:(不适合直接copy,可以简单看一下,很简单)
public com.tfiir.srl.framework.ResponseEntity<UploadBase64Response> upload(@RequestBody UploadBase64Request request) {
UploadBase64Response uploadBase64Response = new UploadBase64Response();
try {
String base64Pic = request.getBase64();
String data = base64Pic.substring(base64Pic.indexOf(",")+1);
System.out.println(data);
//文件存放按日期文件夹存放
Date time=new Date();
String dateUrl=new SimpleDateFormat("YYYY").format(time)+"/"+new SimpleDateFormat("MM").format(time)+"/"+new SimpleDateFormat("dd").format(time)+"/";
// data = "JXU4RkQ5JXU5MUNDJXU2NjJGJXU4OTgxJXU1MkEwJXU1QkM2JXU3Njg0JXU1MTg1JXU1QkI5JXVGRjAxc2FkZmE=";
byte[] strBase64 = new BASE64Decoder().decodeBuffer(data);
try {
InputStream is = new ByteArrayInputStream(strBase64);
BufferedImage image = ImageIO.read(is);
System.out.println(image.getHeight());
System.out.println(image.getWidth());
}catch (Exception e){
e.printStackTrace();
}
FileOutputStream fis = null;
BufferedInputStream bis = null;
String fileType = "";
if (request.getFileType() != null){
fileType = "." + request.getFileType();
}
String filename =UUID.randomUUID().toString() + fileType;
logger.info(filename);
File file = new File("/home/site/file/" +dateUrl+ filename);
File parent = file.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}
try {
fis = new FileOutputStream(file);
fis.write(strBase64, 0, strBase64.length);
uploadBase64Response.setFileUrl(fileUrl+dateUrl+filename);
System.out.println("success");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return com.tfiir.srl.framework.ResponseEntity.success(uploadBase64Response);
} catch (Exception e) {
return ResponseEntity.error(500,e.getMessage());
}
}