java文件操作在项目开发中经常用到,比如前端上送文件流(byte[])到后台并转成文件;或者前端上送base64加码后的文件到后台转成文件。博主在此整理了几个常用方法,希望能帮助大家用得上。
导入包
//导入spring工具包,如果使用spring框架(我想大概率会使用)直接使用FileCopyUtils.java
import org.springframework.util.FileCopyUtils;
//导入IO包
import java.io.*;
//导入base64包
import java.util.Base64;
方法干货
在IO操作中关于关流这块,尽量使用JDK7的特性,将涉及到关流的对象放到try(...)里面,就可以不用写finally块了。
/**
* 文件转二进制
* @param filePath
* @return
*/
public static byte[] file2Bytes(String filePath){
File file = new File(filePath);
if (!file.exists())return new byte[0];
try(InputStream inputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
) {
byte[] buf = new byte[1024];
int len;
if ((len=bufferedInputStream.read(buf))>=0){
outputStream.write(buf,0,len);
}
return outputStream.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return null;
}
/**
* 将二进制流转换为文件
* @param bytes
* @return
*/
public static String bytes2File(byte[] bytes,String fileName){
File file = new File(fileName);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try (FileOutputStream outputStream = new FileOutputStream(fileName);){
outputStream.write(bytes);
return fileName;
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return null;
}
/**
* 将二进制流转换为文件
* @param bytes
* @param fileName
*/
public static void bytes2FileSpring(byte[] bytes,String fileName){
try {
FileCopyUtils.copy(bytes,new File(fileName));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 将文件转base64
* @param filePath 文件路径
*/
public static String fileToBase64(String filePath){
try( InputStream fileInputStream = new FileInputStream(new File(filePath));
BufferedInputStream buf = new BufferedInputStream(fileInputStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();){
int len;
byte [] bytes = new byte[1024];
while ((len=buf.read(bytes))!=-1){
byteArrayOutputStream.write(bytes,0,len);
}
return Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray());
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return null;
}
/**
* 将base64字符串转文件
* @param base64Str base64字符串
*/
public static void base64ToFile(String base64Str,String fileName){
//解码
byte [] bytes = Base64.getDecoder().decode(base64Str);
try (FileOutputStream outputStream = new FileOutputStream(fileName);){
outputStream.write(bytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
base64码表
base64码表,我觉得作为一个程序猿,尤其是多年经验的程序猿,建议背会,像背诵当年的化学元素周期表一样,况且只有64个:
Base64 | |||||||
索引 |
对应字符 |
索引 |
对应字符 |
索引 |
对应字符 |
索引 |
对应字符 |
0 |
A |
17 |
R |
34 |
i |
51 |
z |
1 |
B |
18 |
S |
35 |
j |
52 |
0 |
2 |
C |
19 |
T |
36 |
k |
53 |
1 |
3 |
D |
20 |
U |
37 |
l |
54 |
2 |
4 |
E |
21 |
V |
38 |
m |
55 |
3 |
5 |
F |
22 |
W |
39 |
n |
56 |
4 |
6 |
G |
23 |
X |
40 |
o |
57 |
5 |
7 |
H |
24 |
Y |
41 |
p |
58 |
6 |
8 |
I |
25 |
Z |
42 |
q |
59 |
7 |
9 |
J |
26 |
a |
43 |
r |
60 |
8 |
10 |
K |
27 |
b |
44 |
s |
61 |
9 |
11 |
L |
28 |
c |
45 |
t |
62 |
+ |
12 |
M |
29 |
d |
46 |
u |
63 |
/ |
13 |
N |
30 |
e |
47 |
v | ||
14 |
O |
31 |
f |
48 |
w | ||
15 |
P |
32 |
g |
49 |
x | ||
16 |
Q |
33 |
h |
50 |
y |