FileUtil.java
6.62 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
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
package com.metroapp.forum;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.MimeTypeMap;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.util.Locale;
public class FileUtil {
private static final String TAG = "FileUtil";
public static boolean hasExtentsion(String filename) {
int dot = filename.lastIndexOf('.');
if ((dot > -1) && (dot < (filename.length() - 1))) {
return true;
} else {
return false;
}
}
// 获取文件扩展名
public static String getExtensionName(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot > -1) && (dot < (filename.length() - 1))) {
return filename.substring(dot + 1);
}
}
return "";
}
// 获取文件名
public static String getFileNameFromPath(String filepath) {
if ((filepath != null) && (filepath.length() > 0)) {
int sep = filepath.lastIndexOf('/');
if ((sep > -1) && (sep < filepath.length() - 1)) {
return filepath.substring(sep + 1);
}
}
return filepath;
}
/**
* 获取文件名称 带后缀
* @param filePath
* @return
*/
public static String getFileName(String filePath){
int start=filePath.lastIndexOf("/");
if(start!=-1 ){
return filePath.substring(start+1);
}else{
return filePath;
}
}
/***
* 获取文件类型
*
* @param path 文件路径
* @return 文件的格式
*/
public static String getFileType(String path) {
String str = "";
if (TextUtils.isEmpty(path)) {
return str;
}
int i = path.lastIndexOf('.');
if (i <= -1) {
return str;
}
str = path.substring(i + 1);
return str;
}
// 获取不带扩展名的文件名
public static String getFileNameNoEx(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot > -1) && (dot < (filename.length()))) {
return filename.substring(0, dot);
}
}
return filename;
}
public static String getMimeType(String filePath) {
if (TextUtils.isEmpty(filePath)) {
return "";
}
String type = null;
String extension = getExtensionName(filePath.toLowerCase());
if (!TextUtils.isEmpty(extension)) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
type = mime.getMimeTypeFromExtension(extension);
}
Log.i(TAG, "url:" + filePath + " " + "type:" + type);
// FIXME
if (StringUtil.isEmpty(type) && filePath.endsWith("aac")) {
type = "audio/aac";
}
return type;
}
public enum SizeUnit {
Byte,
KB,
MB,
GB,
TB,
Auto,
}
public static String formatFileSize(long size) {
return formatFileSize(size, SizeUnit.Auto);
}
public static String formatFileSize(long size, SizeUnit unit) {
if (size < 0) {
return "未知大小";
}
final double KB = 1024;
final double MB = KB * 1024;
final double GB = MB * 1024;
final double TB = GB * 1024;
if (unit == SizeUnit.Auto) {
if (size < KB) {
unit = SizeUnit.Byte;
} else if (size < MB) {
unit = SizeUnit.KB;
} else if (size < GB) {
unit = SizeUnit.MB;
} else if (size < TB) {
unit = SizeUnit.GB;
} else {
unit = SizeUnit.TB;
}
}
switch (unit) {
case Byte:
return size + "B";
case KB:
return String.format(Locale.US, "%.2fKB", size / KB);
case MB:
return String.format(Locale.US, "%.2fMB", size / MB);
case GB:
return String.format(Locale.US, "%.2fGB", size / GB);
case TB:
return String.format(Locale.US, "%.2fPB", size / TB);
default:
return size + "B";
}
}
/**
* 获取文件MD5的值
*
* @param file 文件
* @return 文件的MD5
*/
public static String getFileMD5(File file) {
if (!file.isFile()) {
return null;
}
MessageDigest digest = null;
FileInputStream in = null;
byte buffer[] = new byte[1024];
int len;
try {
digest = MessageDigest.getInstance("MD5");
in = new FileInputStream(file);
while ((len = in.read(buffer, 0, 1024)) != -1) {
digest.update(buffer, 0, len);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
BigInteger bigInt = new BigInteger(1, digest.digest());
return bigInt.toString(16);
}
public static void deleteFolder(File folder) {
if (folder.isDirectory()) {
for (File file : folder.listFiles()) {
if (file.isDirectory()) {
deleteFolder(file);
} else {
file.delete();
}
}
}
folder.delete();
}
/**
* 下载文件至本地
*
* @param remoteUrl 远程文件地址
* @param localFile 本地保存路径
* @throws IOException
*/
public static void downloadFile(String remoteUrl, File localFile) throws IOException {
int fileSize;
URL url = new URL(remoteUrl);
// 打开连接
URLConnection con = url.openConnection();
//获得文件的长度
fileSize = con.getContentLength();
// 输入流
InputStream is = con.getInputStream();
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流
OutputStream os = new FileOutputStream(localFile);
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
// 完毕,关闭所有链接
os.close();
is.close();
}
}