把前端经过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-11-15AntDesign项目实战:新手入门与初级应用教程
- 2024-11-15AntDesign-Form-rules项目实战:新手指南
- 2024-11-14ESLint课程:初学者指南
- 2024-11-14Form.List 动态表单课程:新手入门教程
- 2024-11-14Redux课程:新手入门完全指南
- 2024-11-13MobX 使用入门教程:轻松掌握前端状态管理
- 2024-11-12前端编程资料:新手入门指南与初级教程
- 2024-11-12前端开发资料入门指南
- 2024-11-12前端培训资料:适合新手与初级用户的简单教程
- 2024-11-12前端入门资料:新手必读指南