1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package com.yanzuoguang.util.helper;
import com.yanzuoguang.util.YzgError;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* 登录相关函数
*
* @author 颜佐光
*/
public class FileHelper {
public static final int FILE_TYPE_NONE = 0;
public static final int FILE_TYPE_IMAGE = 1;
public static final int FILE_TYPE_VIDEO = 2;
public static final int FILE_TYPE_AUDIO = 3;
/**
* 获取文件类型
*
* @param filePath 来源文件
* @return
* @throws IOException
*/
public static String getMimeType(String filePath) {
File file = new File(filePath);
if (!file.exists()) {
return StringHelper.EMPTY;
}
Path path = Paths.get(filePath);
try {
String contentType = Files.probeContentType(path);
return contentType;
} catch (Exception ex) {
throw YzgError.getRuntimeException(ex,"044",filePath);
}
}
/**
* 获取文件媒体类型
*
* @param file
* @return
* @throws IOException
*/
public static int getMediaType(String file) {
String mimeType = FileHelper.getMimeType(file);
if (mimeType == null) {
throw YzgError.getRuntimeException("008",file);
}
int type = getMediaTypeByMime(mimeType);
// System.out.println(mimeType);
if (type == FILE_TYPE_NONE) {
throw YzgError.getRuntimeException("009");
}
return type;
}
/**
* 获取视频类型
*
* @param mimeType
* @return
*/
public static int getMediaTypeByMime(String mimeType) {
mimeType = StringHelper.getFirst(mimeType, StringHelper.EMPTY);
if (mimeType.startsWith("video/")) {
return FILE_TYPE_VIDEO;
} else if (mimeType.startsWith("audio/")) {
return FILE_TYPE_AUDIO;
} else if (mimeType.startsWith("image/")) {
return FILE_TYPE_IMAGE;
} else {
return FILE_TYPE_NONE;
}
}
/**
* 创建目录
*
* @param path 文件路径
* @return 读取的内容
*/
public static String createDirectory(String path) {
return createDirectory(new File(path));
}
/**
* 创建目录
*
* @param file 文件路径
* @return 读取的内容
*/
public static String createDirectory(File file) {
if (file.exists()) {
//检查此路径名的文件是否是一个目录(文件夹)
if (file.isDirectory()) {
return StringHelper.EMPTY;
} else {
throw YzgError.getRuntimeException("010",file.getName());
}
}
file.mkdirs();
return file.getName();
}
/**
* 获取登录标记
*
* @param file 文件路径
* @param encoding 文件编码
* @return 读取的内容
*/
public static String readFile(File file, String encoding) {
//检查此路径名的文件是否是一个目录(文件夹)
if (file.isDirectory()) {
return StringHelper.EMPTY;
}
if (!file.exists()) {
return StringHelper.EMPTY;
}
try {
InputStream inputStream = new FileInputStream(file);
return readFile(inputStream, encoding);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 读取文件内容
*
* @param inputStream 文件流
* @param encoding 文件编码
* @return 文件内容的结果
*/
public static String readFile(InputStream inputStream, String encoding) {
if (inputStream == null) {
return StringHelper.EMPTY;
}
StringBuilder sb = new StringBuilder();
try {
try {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, encoding);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line;
//分行读取
while ((line = bufferedReader.readLine()) != null) {
if (sb.length() > 0) {
sb.append("\n");
}
sb.append(line);
}
} finally {
inputStream.close();//关闭输入流
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
return sb.toString();
}
/**
* 写入文件内容
*
* @param file 文件路径
* @param content 文件内容
* @param encoding 文件编码
*/
public static void writeFileAppend(File file, String content, String encoding) {
try {
// 创建文件夹
File parentFile = file.getParentFile();
if (parentFile != null) {
if (!parentFile.exists()) {
if (!parentFile.mkdirs()) {
throw YzgError.getRuntimeException("011");
}
}
if (!parentFile.exists()) {
throw YzgError.getRuntimeException("011");
}
}
// 文件不存在则创建
if (!file.exists()) {
file.createNewFile();
}
// 写入文件
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
raf.seek(file.length());
raf.write(content.getBytes(encoding));
raf.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 写入文件内容
*
* @param file 文件路径
* @param content 文件内容
* @param encoding 文件编码
*/
public static void writeFile(File file, String content, String encoding) {
try {
// 删除文件
if (file.exists()) {
if (!file.delete()) {
throw YzgError.getRuntimeException("012");
}
}
if (file.exists()) {
throw YzgError.getRuntimeException("012");
}
writeFileAppend(file, content, encoding);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 根据路径删除指定的目录或文件,无论存在与否
*
* @param sPath 要删除的目录或文件
* @return 删除成功返回 true,否则返回 false。
*/
public static boolean deleteFolder(String sPath) {
if (StringHelper.isEmpty(sPath)) {
return false;
}
boolean flag = false;
File file = new File(sPath);
// 判断目录或文件是否存在
if (!file.exists()) {
// 不存在返回 false
return flag;
} else {
// 判断是否为文件
if (file.isFile()) {
// 为文件时调用删除文件方法
return deleteFile(sPath);
} else {
// 为目录时调用删除目录方法
return deleteDirectory(sPath);
}
}
}
/**
* 删除单个文件
*
* @param sPath 被删除文件的文件名
* @return 单个文件删除成功返回true,否则返回false
*/
public static boolean deleteFile(String sPath) {
boolean flag = false;
File file = new File(sPath);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
return flag;
}
/**
* 删除目录(文件夹)以及目录下的文件
*
* @param sPath 被删除目录的文件路径
* @return 目录删除成功返回true,否则返回false
*/
public static boolean deleteDirectory(String sPath) {
//如果sPath不以文件分隔符结尾,自动添加文件分隔符
if (!sPath.endsWith(File.separator)) {
sPath = sPath + File.separator;
}
File dirFile = new File(sPath);
//如果dir对应的文件不存在,或者不是一个目录,则退出
if (!dirFile.exists() || !dirFile.isDirectory()) {
return false;
}
boolean flag = true;
//删除文件夹下的所有文件(包括子目录)
File[] files = dirFile.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
//删除子文件
if (files[i].isFile()) {
flag = deleteFile(files[i].getAbsolutePath());
if (!flag) {
break;
}
} //删除子目录
else {
flag = deleteDirectory(files[i].getAbsolutePath());
if (!flag) {
break;
}
}
}
}
if (!flag) {
return false;
}
//删除当前目录
if (dirFile.delete()) {
return true;
} else {
return false;
}
}
}