后端MultipartFile接收文件转Base64
@[toc]
背景说明
最开始写的版本代码删改较多且无法运行,在其他博主给我指出问题及改进措施之后,下面的是最新汇总并测试结果有效的接口,编写日期:2022年08月12日。
下面代码是后端接收到文件,并把文件转为Base64,而不是前端传Base64传递给后端,这点需注意!
如果想前端传递Base64的需自己找文章,主要:网上的部分文章转Base64没给pom依赖,会导致找不到类,我这个是能够进行编译Base64的工具包,也注明了import的引入,记录改文章也是方便后续我自己查看而已。
测试案例+代码
测试接口1和测试接口2没太大差别,一个用了buffer缓存一个没用到而已,最终结果打印一致。
pom依赖
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
测试方法1
测试方法1
import com.example.demo.bean.OperateTeacherReq;
import com.example.demo.bean.TeacherReq;
import com.example.demo.config.LogAnnotation;
import com.example.demo.mapper.TeacherMapper;
import com.example.demo.response.Response;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
@RestController
public class Controller {
//测试方法1
@RequestMapping(value = "uploadLogo")
public void uploadLogo(@RequestParam("file") MultipartFile fileImage) {
String base64EncoderImg = "";
try {
base64EncoderImg = Base64.encodeBase64String(fileImage.getBytes());
base64EncoderImg = "data:image/png;base64," + base64EncoderImg;
System.out.println(base64EncoderImg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
用postman或者apipost模拟发送接口测试结果,随便上传一个excel文件
测试方法2
测试方法2
import com.example.demo.bean.OperateTeacherReq;
import com.example.demo.bean.TeacherReq;
import com.example.demo.config.LogAnnotation;
import com.example.demo.mapper.TeacherMapper;
import com.example.demo.response.Response;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
@RestController
public class Controller {
//测试方法2
@RequestMapping(value = "uploadLogo2")
public void uploadLogo2(@RequestParam("file") MultipartFile fileImage) {
String base64EncoderImg = "";
byte[] buffer = null;
try {
InputStream inputStream = fileImage.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = inputStream.read(b)) != -1)
{
bos.write(b, 0, n);
}
inputStream.close();
bos.close();
buffer = bos.toByteArray();
base64EncoderImg = Base64.encodeBase64String(buffer);
base64EncoderImg = "data:image/png;base64," + base64EncoderImg;
System.out.println(base64EncoderImg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
用postman或者apipost模拟发送接口测试结果,随便上传一个excel文件
打印2个测试接口输出结果相同:
代码输出格式为:"data:image/png;base64," + base64EncoderImg;,结果和代码一致,确认有效
data:image/png;base64,UEsDBBQACAgIAAlOjlMAAAAAAAAAAAAAAAALAAAAX3JlbHMvLnJlbHOtksFqwzAMhl/F6N447WCMUbeXMuhtjO4BNFtJTGLL2NqWvf3MLltLChvsKCR9/wfSdj+HSb1RLp6jgXXTgqJo2fnYG3g+PazuQBXB6HDiSAYiw363faIJpW6UwaeiKiIWA4NIute62IECloYTxdrpOAeUWuZeJ7Qj9qQ3bXur808GnDPV0RnIR7cGdcLckxiYJ/3OeXxhHpuKrY2PRL8J5a7zlg5sXwNFWci+mAC97LL5dnFsHzPXTUzpv2VoFoqO3CrVBMriqVwzulkwspzpb0rXj6IDCToU/KJeCOmzH9h9AlBLBwinjHq94wAAAEkCAABQSwMEFAAICAgACU6OUwAAAAAAAAAAAAAAABMAAABbQ29ud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