Java中File、Base64、MultipartFile之間相互轉(zhuǎn)換的代碼詳解
更新時(shí)間:2024年04月03日 11:40:51 作者:笑小楓
File、Base64和MultipartFile都是在編程中常用的類或者數(shù)據(jù)類型,用于處理文件和數(shù)據(jù)的存儲(chǔ)、傳輸和轉(zhuǎn)換等操作,本文將給大家介紹了Java中File、Base64、MultipartFile之間相互轉(zhuǎn)換,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下
文件轉(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;
}
}
// 處理過(guò)后的數(shù)據(jù)通過(guò)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)文章
Java中比較器Comparator和Comparable的區(qū)別
這篇文章主要介紹了Java中比較器Comparator和Comparable的區(qū)別,我們?cè)谑褂?Collections.sort()對(duì)鏈表進(jìn)行排序時(shí),常常需要根據(jù)不同情況自定義排序規(guī)則,今天我們來(lái)看看比較器之間的區(qū)別,需要的朋友可以參考下2023-08-08
Java8新日期時(shí)間API的20個(gè)使用示例
這篇文章主要介紹了Java8新日期時(shí)間API的20個(gè)使用示例,為了學(xué)習(xí)Java 8的這個(gè)新庫(kù),這里我創(chuàng)建了20個(gè)以任務(wù)為導(dǎo)向的例子,需要的朋友可以參考下2015-03-03
springboot整合mybatis-plus 實(shí)現(xiàn)分頁(yè)查詢功能
這篇文章主要介紹了springboot整合mybatis-plus 實(shí)現(xiàn)分頁(yè)查詢功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09

