Java图片上传Base64 类型上传 后台转换工具类
java图片上传Base64 类型上传 后台转换工具类
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;import org.springframework.web.multipart.MultipartFile;import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;public class BASE64DecodedMultipartFile implements MultipartFile {private final byte[] imgContent;private final String header;public BASE64DecodedMultipartFile(byte[] imgContent, String header) {this.imgContent = imgContent;this.header = header.split(";")[0];}@Overridepublic String getName() {// TODO - implementation depends on your requirementsreturn System.currentTimeMillis() + Math.random() + "." + header.split("/")[1];}@Overridepublic String getOriginalFilename() {// TODO - implementation depends on your requirementsreturn System.currentTimeMillis() + (int)Math.random() * 10000 + "." + header.split("/")[1];}@Overridepublic String getContentType() {// TODO - implementation depends on your requirementsreturn header.split(":")[1];}@Overridepublic boolean isEmpty() {return imgContent == null || imgContent.length == 0;}@Overridepublic long getSize() {return imgContent.length;}@Overridepublic byte[] getBytes() throws IOException {return imgContent;}@Overridepublic InputStream getInputStream() throws IOException {return new ByteArrayInputStream(imgContent);}@Overridepublic void transferTo(File dest) throws IOException, IllegalStateException {new FileOutputStream(dest).write(imgContent);}/***将图片Base64 码转成上传流*/public static MultipartFile base64ToMultipart(String base64) {try {String[] baseStrs = base64.split(",");BASE64Decoder decoder = new BASE64Decoder();byte[] b = new byte[0];b = decoder.decodeBuffer(baseStrs[1]);for(int i = 0; i < b.length; ++i) {if (b[i] < 0) {b[i] += 256;}}return new BASE64DecodedMultipartFile(b, baseStrs[0]);} catch (IOException e) {e.printStackTrace();return null;}}//base64字符串转byte[]public static byte[] base64String2Byte(String base64Str) throws IOException{BASE64Decoder decoder = new BASE64Decoder();return decoder.decodeBuffer(base64Str);}
// //byte[]转base64public static String byte2Base64String(byte[] b){BASE64Encoder encoder = new BASE64Encoder();return encoder.encode(b);}}
Java图片上传Base64 类型上传 后台转换工具类相关推荐
- java字体颜色编程_Java 字体颜色转换工具类 ColorUtil
Java 字体颜色转换工具类 ColorUtil,Java 字体颜色转换工具类 ColorUtil-- import java.awt.Color; import jxl.format.Colour; ...
- Java 字体颜色转换工具类 ColorUtil
import java.awt.Color; import jxl.format.Colour; /** *字体颜色转换工具类 * @author tanghui * */ pu ...
- Java中 LocalDate、LocalTime、LocalDateTime三个时间工具类的使用介绍
Java中 LocalDate.LocalTime.LocalDateTime三个时间工具类的使用介绍 一.背景: 之前在做项目的过程中,对日期时间类没有一个系统的了解,总是在用的时候去搜索一下,解决 ...
- 【Java代码】坐标系说明+WGS84\GCJ02\BD09坐标系转换工具+Java坐标系转换及验证源代码分享(粘贴可用)
1. 坐标系说明 开发地图应用服务时,一定会接触到各种坐标系,而保证坐标系的正确与合理是一切数据分析的前提,总的来说,坐标系可以分为两大类:地理坐标系GCS(Geographic Coordinate ...
- java hh24miss_时间日期转换工具类,获取当前时间YYYYMMDD24HHMISS、YYYYMMDDHHMISS
packagecom.ztesoft.iotcmp.util;importcom.ztesoft.zsmart.core.log.ZSmartLogger;importjava.sql.Timesta ...
- 自定义java对象转换工具类
背景 项目中经常有VO.PO.DTO等之间转换,由于apache工具类中BeanUtils.copyProperties及Json序列化反序列化方式转换性能比较低(阿里巴巴规范检查有提示不建议采用). ...
- java 视频转码工具类_JavaCV入门指南:FrameConverter转换工具类及CanvasFrame图像预览工具类(javaCV教程完结篇)...
JavaCV入门指南:FrameConverter转换工具类及CanvasFrame图像预览工具类(javaCV教程完结篇) 前言 再此章之前,我们已经详细介绍和剖析了javacv的结构和ffmpeg ...
- Java金额转换工具类
package com.healthy.prms.common.util;import java.math.BigDecimal;/*** @ClassName: RMBUtils* @Descrip ...
- Java时间戳与日期格式转换工具类
Java时间戳与日期格式转换工具类 在做web开发时,有时候需要用到时间戳,在前台进行日期转换,从数据库中取出来是日期格式的,这里记录下使用的转换工具类(这些都是静态方法,通过类名.方法的形式即可调用 ...
- java dto 转换_json dto javabean转换工具类
[java]代码库package com.slb.webservice.servlet; import java.io.IOException; import java.util.ArrayList; ...
最新文章
- ssm 异常捕获 统一处理_SpringMVC 统一异常处理介绍及实战
- (转)PHP框架大比武
- 服务器端执行javascript脚本
- oracle使用sqlplus创建表空间
- 链接哈希是什么_熊猫人行情:什么是工作量证明机制(POW)
- 20非常有用的Java程序片段(3)
- C++ 基类和派生类的构造函数
- 博文写作——摘要摘要图标
- 在dos下或web下Fatal Error: undefined function mysql_connect()
- arguments.callee 调用自身
- 贾跃亭发布最新PPT:FF商业计划书
- visio给图片添加任意形状外框
- ansible模块专项练习题
- SQP(序列二次规划中的Marotos效应)
- 使用vs2008制作dll文件,生成动态链接库
- 深度学习 yolov5等结构图
- 回顾之前给公司的建议,及离开前想说的话
- 极其简单的Flutter 屏幕适配
- 蓝桥杯 算法训练 区间k大数查询 --c++
- C语言指针类型的意义