把前端经过Base64算法转换的图片字符串处理为图片文件并保存的关键代码
2022/4/14 22:12:53
本文主要是介绍把前端经过Base64算法转换的图片字符串处理为图片文件并保存的关键代码,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
public class CropsImageVo { //作物生长全局图片 private String imageBase64; public String getImageBase64() { return imageBase64; } public void setImageBase64(String imageBase64) { this.imageBase64 = imageBase64; } @Override public String toString() { return "CropsImageVo{" + "imageBase64='" + imageBase64 + '\'' + '}'; } }
import sun.misc.BASE64Decoder; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; public class Base64Util { public static File base64ConvertFile(String s) { String filePath = "D:\\myofficework\\resouce\\"; String fileName = System.currentTimeMillis()+".jpg"; BASE64Decoder decoder = new BASE64Decoder(); File file = null; try { byte[] bytes = decoder.decodeBuffer(s); for (int i = 0; i < bytes.length; ++i) { if (bytes[i] < 0) { bytes[i] += 256; } } String imageFilePath = filePath+fileName; //System.out.println("************" + imageFilePath22); //String imageFilePath = filePath+fileName.replace("\\\\","/"); //System.out.println("************" + imageFilePath); OutputStream out = new FileOutputStream(imageFilePath); out.write(bytes); out.flush(); out.close(); System.out.println("图片上传成功"); file = new File(imageFilePath); }catch (IOException e){ e.printStackTrace(); } return file; } }
@PostMapping("imageUpload") public AjaxResult imageUpload(@RequestBody CropsImageVo cropsImageVo) throws FileNotFoundException { //替换掉部分图片字符串信息 String imageBase64 = cropsImageVo.getImageBase64().replace("data:image/jpeg;base64,", ""); //System.out.println(imageBase64); //保存图片并返回图片文件对象 File file = Base64Util.base64ConvertFile(imageBase64); StorePath storePath = this.fastFileStorageClient.uploadImageAndCrtThumbImage(new FileInputStream(file),file.length(),"jpg",null); return AjaxResult.success(address+storePath.getFullPath()); }
这篇关于把前端经过Base64算法转换的图片字符串处理为图片文件并保存的关键代码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-25前端大厂面试真题解析与实战攻略
- 2024-12-25如何准备前端面试:新手指南
- 2024-12-25前端面试题详解与实战攻略
- 2024-12-25前端面试真题详解与实战攻略
- 2024-12-252024前端大厂面试真题详解及备考指南
- 2024-12-252024前端面试必备指南:从零开始掌握前端面试技巧
- 2024-12-252024前端面试题详解与实战指南
- 2024-12-21动态面包屑教程:新手入门指南
- 2024-12-21动态主题处理教程:新手必读指南
- 2024-12-21富文本编辑器教程:新手入门指南