FileUtils.java
1.8 KB
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
package com.framework.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FileUtils {
private static String[] imageType = {"jpg", "bmp", "gif", "png", "ico" };
/** 截取文件后缀名 */
public static String getSuffixName(String oriName) {
oriName = oriName.substring(oriName.lastIndexOf("."), oriName.length());
return oriName;
}
public static File genOutFile(File ori, int w, int h) {
String oriName = ori.getName();
if (oriName != null && oriName.length() > 0) {
String name = oriName.substring(0, oriName.lastIndexOf('.'));
String suffix = FileUtils.getSuffixName(oriName);
return new File(ori.getParentFile(), name + "_" + w + "_" + h + suffix);
}
return null;
}
public static File genOutFile(File ori) {
String oriName = ori.getName();
if (oriName != null && oriName.length() > 0) {
String name = oriName.substring(0, oriName.lastIndexOf('.'));
String suffix = FileUtils.getSuffixName(oriName);
return new File(ori.getParentFile(), name + suffix);
}
return null;
}
public static void copyFile(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
public static boolean isImageFile(String fileName) {
boolean f = false;
String suffix = getSuffixName(fileName);
suffix = suffix.toLowerCase();
for (int i = 0; i < imageType.length; i++) {
if (suffix.indexOf(imageType[i]) > 0) {
f = true;
}
}
return f;
}
}