0
点赞
收藏
分享

微信扫一扫

Java语言File,String, byte[]及File和MultipartFile相互转化


概述

起因:在​​使用Selenium实现截图​​时,发现API有三种方式生成截图:

byte[] img = driver.getScreenshotAs(OutputType.BYTES);
String img = driver.getScreenshotAs(OutputType.BASE64);
File img = driver.getScreenshotAs(OutputType.FILE);

于是有了三种方式相互转化的需求。

String与byte[]相互转化

最简单:

// String to byte[]
byte[] aa = "ss".getBytes();
// byte[] to String
Arrays.toString(aa);

String与File相互转化

// File to String
public String fileToBase64(File file) {
String base64 = null;
InputStream in = null;
try {
in = new FileInputStream(file);
byte[] bytes = new byte[(int) file.length()];
in.read(bytes);
base64 = Base64.getEncoder().encodeToString(bytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return base64;
}

// String to File
public void base64ToFile(String destPath, String base64, String fileName) {
File file;
//创建文件目录
File dir = new File(destPath);
if (!dir.exists() && !dir.isDirectory()) {
dir.mkdirs();
}
BufferedOutputStream bos = null;
java.io.FileOutputStream fos = null;
try {
byte[] bytes = Base64.getDecoder().decode(base64);
file = new File(destPath + "/" + fileName);
fos = new java.io.FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

byte[]与File相互转化

// byte[] to File
public void byte2File(byte[] buf, String filePath, String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file;
try {
File dir = new File(filePath);
if (!dir.exists() && dir.isDirectory()) {
dir.mkdirs();
}
file = new File(filePath + File.separator + fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(buf);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

// File to byte[]
public byte[] file2byte(File file) {
byte[] buffer = 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);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}

File和MultipartFile相互转化

File转换成MultipartFile

需要引入依赖:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>

代码:

FileInputStream fileInputStream = new FileInputStream(file);
MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(), ContentType.IMAGE_PNG.toString(), fileInputStream);

MultipartFile转换成File

需要引入依赖:

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>

代码:

File file = new File(path); 
FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), file);


举报

相关推荐

0 条评论