Java中File、Base64、MultipartFile之間相互轉(zhuǎn)換的代碼詳解
文件轉(zhuǎn)base64字符串
/**
* file轉(zhuǎn)換為base64
* 注意:這里轉(zhuǎn)換為base64后,是不包含文件head頭的
*/
public static String fileToBase64(File file) {
Base64.Encoder base64 = Base64.getEncoder();
String base64Str = null;
try (FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
base64Str = base64.encodeToString(bos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
return base64Str;
}
base64字符串轉(zhuǎn)文件
/**
* base64轉(zhuǎn)化為file,并保存到指定路徑下
*/
public static void base64ToFile(String base, String path) {
if (StringUtils.isBlank(base)) {
return;
}
Base64.Decoder decoder = Base64.getDecoder();
try (OutputStream out = new FileOutputStream(path)) {
byte[] bytes = decoder.decode(base);
for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {
bytes[i] += 256;
}
}
out.write(bytes);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
base64轉(zhuǎn)化為file流
/**
* base64轉(zhuǎn)化為file流
*/
public static File base64ToFile(String base64) {
if (base64 == null || "".equals(base64)) {
return null;
}
byte[] buff = Base64.getDecoder().decode(base64);
File file;
try {
file = File.createTempFile("tmp", null);
} catch (IOException e) {
e.printStackTrace();
return null;
}
try (FileOutputStream out = new FileOutputStream(file)) {
out.write(buff);
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
base64轉(zhuǎn)MultipartFile
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.Base64;
/**
* @author 笑小楓
*/
public class Base64DecodedMultipartFile implements MultipartFile {
private final byte[] imgContent;
private final String header;
private final String fileName;
public Base64DecodedMultipartFile(byte[] imgContent, String header, String fileName) {
this.imgContent = imgContent;
this.header = header.split(";")[0];
this.fileName = fileName;
}
@Override
public String getName() {
return fileName + "." + header.split("/")[1];
}
@Override
public String getOriginalFilename() {
return fileName + "." + header.split("/")[1];
}
@Override
public String getContentType() {
return header.split(":")[1];
}
@Override
public boolean isEmpty() {
return imgContent == null || imgContent.length == 0;
}
@Override
public long getSize() {
return imgContent.length;
}
@Override
public byte[] getBytes() {
return imgContent;
}
@Override
public InputStream getInputStream() {
return new ByteArrayInputStream(imgContent);
}
@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
try (FileOutputStream fos = new FileOutputStream(dest)) {
fos.write(imgContent);
}
}
/**
* base64轉(zhuǎn)multipartFile
**/
public static MultipartFile base64Convert(String base64, String header, String fileName) {
Base64.Decoder decoder = Base64.getDecoder();
byte[] b = decoder.decode(base64);
//取索引為1的元素進(jìn)行處理
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
// 處理過后的數(shù)據(jù)通過Base64DecodeMultipartFile轉(zhuǎn)換為MultipartFile對(duì)象
return new Base64DecodedMultipartFile(b, header, fileName);
}
}
byte數(shù)組轉(zhuǎn)MultiPartFile
- POM導(dǎo)入
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>RELEASE</version>
</dependency>
- 代碼部分
byte[] bytes = message.getPacket(); InputStream inputStream = new ByteArrayInputStream(bytes); MultipartFile file = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
MultipartFile轉(zhuǎn)化為byte數(shù)組
byte[] bytes= file.getBytes();
以上就是Java中File、Base64、MultipartFile之間相互轉(zhuǎn)換的代碼詳解的詳細(xì)內(nèi)容,更多關(guān)于Java File、Base64、MultipartFile轉(zhuǎn)換的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
淺談collection標(biāo)簽的oftype屬性能否為java.util.Map
這篇文章主要介紹了collection標(biāo)簽的oftype屬性能否為java.util.Map,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
為spring get請(qǐng)求添加自定義的參數(shù)處理操作(如下劃線轉(zhuǎn)駝峰)
這篇文章主要介紹了為spring get請(qǐng)求添加自定義的參數(shù)處理操作(如下劃線轉(zhuǎn)駝峰),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Spring?Security?OAuth?Client配置加載源碼解析
這篇文章主要為大家介紹了Spring?Security?OAuth?Client配置加載源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Java統(tǒng)計(jì)字符串中字符出現(xiàn)次數(shù)的方法示例
這篇文章主要介紹了Java統(tǒng)計(jì)字符串中字符出現(xiàn)次數(shù)的方法,涉及Java針對(duì)字符串的遍歷、查找、計(jì)算等相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
spring?cloud?eureka注冊(cè)原理-注冊(cè)失敗填坑筆記
這篇文章主要介紹了spring?cloud?eureka注冊(cè)原理-注冊(cè)失敗填坑筆記,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05

