在Spring Boot中,将Base64编码的字符串转换为MultipartFile和FileInputStream可按照以下方式进行:
转MultipartFile
将Base64编码的字符串转换为字节数组byte[]:
byte[] bytes = Base64.getDecoder().decode(base64String);
将字节数组byte[]转换为MultipartFile对象:
MultipartFile multipartFile = new MockMultipartFile("filename", bytes);
请注意,此处使用了MockMultipartFile类。如果您正在处理实际上传的文件,请使用CommonsMultipartFile或StandardMultipartFile类。
转FileInputStream
将字节数组byte[]转换为FileInputStream对象:
FileInputStream fileInputStream = new FileInputStream(new File("filename"));
fileInputStream.read(bytes);
请注意,此处的“filename”是您要创建的文件名。如果您希望从现有文件中获取FileInputStream,则将其替换为文件路径即可。
评论区