cloudroam
2025-03-10 c306cba52bcc3e2c423f77d4a52c35ad04c52038
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
package com.jsh.erp.utils;
 
import java.io.*;
import java.util.*;
 
/**
 *
 * 文件处理工具类
 *
 */
public class FileUtils {
 
    /**
     * 功 能: 创建文件夹
     *
     * @param path
     *            参 数:要创建的文件夹名称
     * @return 返回值: 如果成功true;否则false 如:FileUtils.mkdir("/usr/apps/upload/");
     */
    public static boolean makedir(String path) {
        File file = new File(path);
        if (!file.exists())
            return file.mkdirs();
        else
            return true;
    }
 
    /**
     * 保存文件
     *
     * @param stream
     * @param path
     *            存放路径
     * @param filename
     *            文件名
     * @throws IOException
     */
    public static void SaveFileFromInputStream(InputStream stream, String path, String filename)
            throws IOException {
        File file = new File(path);
        boolean flag=true;
        if(!file.exists()){
            flag=file.mkdirs();
        }
        if(flag){
            FileOutputStream fs = new FileOutputStream(new File(path+filename));
            byte[] buffer = new byte[1024 * 1024];
            int byteread = 0;
            while ((byteread = stream.read(buffer)) != -1) {
                fs.write(buffer, 0, byteread);
                fs.flush();
            }
            fs.close();
            stream.close();
        }
    }
 
 
    /**
     * 列出某个目录下的所有文件,子目录不列出
     * @param folderPath:文件夹路径
     * @return
     */
    public static List<String> listFile(String folderPath){
        List<String> fileList = new ArrayList<String>(); //FileViewer.getListFiles(destPath, null, false);
        File f = new File(folderPath);
        File[] t = f.listFiles();
        for(int i = 0; i < t.length; i++){
            fileList.add(t[i].getAbsolutePath());
        }
        return fileList;
    }
 
 
    /**
     * 判断文件是否存在
     *
     * @param fileName
     * @return
     */
    public static boolean exists(String fileName) {
        File file = new File(fileName);
        if (file.exists()) {
            return true;
        } else {
            return false;
        }
    }
 
    /**
     * 获取文件扩展名
     *
     * @param fileName
     * @return
     * */
    public static String getFileExtendName(String fileName) {
        if (fileName == null) {
            return "";
        } else {
            return fileName.substring(fileName.lastIndexOf(".") + 1, fileName
                    .length());
        }
    }
 
    /**
     * 创建一个新文件,如果存在则报错
     *
     * @param filePath
     * @param fileName
     * @return
     */
    public static void createFile(String filePath, String fileName)
            throws RuntimeException {
        String file = null;
        if (filePath == null) {
            file = fileName;
        } else {
            file = filePath + File.separator + fileName;
        }
        createFile(file);
    }
 
    /**
     * 创建一个新文件(含路径),如果存在则报错
     *
     * @param fileName
     *            含有路径的文件名
     * @return
     */
    public static void createFile(String fileName) throws RuntimeException {
        File f = new File(fileName);
        if (f.exists()) {
            throw new RuntimeException("FILE_EXIST_ERROR");
        } else {
            try {
                File fileFolder = f.getParentFile();
                if (!fileFolder.exists())
                    fileFolder.mkdirs();
                f.createNewFile();
            } catch (IOException ie) {
                System.out.println("文件" + fileName + "创建失败:" + ie.getMessage());
                throw new RuntimeException("FILE_CREATE_ERROR");
            }
        }
    }
 
 
    /**
     * 创建目录,如果存在则不创建
     *
     * @param path
     * @return 返回结果null则创建成功,否则返回的是错误信息
     * @return
     */
    public static String createDir(String path, boolean isCreateSubPah) {
        String msg = null;
        File dir = new File(path);
 
        if (dir == null) {
            msg = "不能创建空目录";
            return msg;
        }
        if (dir.isFile()) {
            msg = "已有同名文件存在";
            return msg;
        }
        if (!dir.exists()) {
            if (isCreateSubPah && !dir.mkdirs()) {
                msg = "目录创建失败,原因不明";
            } else if (!dir.mkdir()) {
                msg = "目录创建失败,原因不明";
            }
        }
        return msg;
    }
 
    /**
     * 删除指定目录或文件。 如果要删除是目录,同时删除子目录下所有的文件
     *
     * @file:File 目录
     * */
    public static void delFileOrFolder(String fileName) {
        if (!exists(fileName))
            return;
        File file = new File(fileName);
        delFileOrFolder(file);
    }
 
    /**
     * 删除指定目录或文件。 如果要删除是目录,同时删除子目录下所有的文件
     *
     * @file:File 目录
     * */
    public static void delFileOrFolder(File file) {
        if (!file.exists())
            return;
        if (file.isFile()) {
            file.delete();
        } else {
            File[] sub = file.listFiles();
            if (sub == null || sub.length <= 0) {
                file.delete();
            } else {
                for (int i = 0; i < sub.length; i++) {
                    delFileOrFolder(sub[i]);
                }
                file.delete();
            }
        }
    }
 
    /**
     * 从Properties格式配置文件中获取所有参数并保存到HashMap中。
     * 配置中的key值即map表中的key值,如果配置文件保存时用的中文,则返回结果也会转成中文。
     *
     * @param file
     * @return
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    public static HashMap readPropertyFile(String file, String charsetName) throws IOException {
        if (charsetName==null || charsetName.trim().length()==0){
            charsetName="gbk";
        }
        HashMap map = new HashMap();
        InputStream is =null;
        if(file.startsWith("file:"))
            is=new FileInputStream(new File(file.substring(5)));
        else
            is=FileUtils.class.getClassLoader().getResourceAsStream(file);
        Properties properties = new Properties();
        properties.load(is);
        Enumeration en = properties.propertyNames();
        while (en.hasMoreElements()) {
            String key = (String) en.nextElement();
            String code = new String(properties.getProperty(key).getBytes(
                    "ISO-8859-1"), charsetName);
            map.put(key, code);
        }
        return map;
    }
    /**
     *
     * @param path
     *            文件路径
     * @param suffix
     *            后缀名
     * @param isdepth
     *            是否遍历子目录
     * @return
     */
    @SuppressWarnings("unchecked")
    public static List getListFiles(String path, String suffix, boolean isdepth) {
        File file = new File(path);
        return FileUtils.listFile(file, suffix, isdepth);
    }
 
    /**
     * @param f
     * @param suffix:后缀名
     * @param isdepth:是否遍历子目录
     * @return
     */
    @SuppressWarnings("unchecked")
    public static List listFile(File f, String suffix, boolean isdepth) {
        // 是目录,同时需要遍历子目录
        List<String> fileList = new ArrayList<String>();
        if (f.isDirectory() && isdepth == true) {
            File[] t = f.listFiles();
            for (int i = 0; i < t.length; i++) {
                listFile(t[i], suffix, isdepth);
            }
        } else {
            String filePath = f.getAbsolutePath();
 
            if (suffix != null) {
                int begIndex = filePath.lastIndexOf(".");// 最后一个.(即后缀名前面的.)的索引
                String tempsuffix = "";
 
                if (begIndex != -1)// 防止是文件但却没有后缀名结束的文件
                {
                    tempsuffix = filePath.substring(begIndex + 1, filePath
                            .length());
                }
 
                if (tempsuffix.equals(suffix)) {
                    fileList.add(filePath);
                }
            } else {
                // 后缀名为null则为所有文件
                fileList.add(filePath);
            }
 
        }
 
        return fileList;
    }
 
    /**
     * 判断文件名是否带盘符,重新处理
     * @param fileName
     * @return
     */
    public static String getFileName(String fileName){
        //判断是否带有盘符信息
        // Check for Unix-style path
        int unixSep = fileName.lastIndexOf('/');
        // Check for Windows-style path
        int winSep = fileName.lastIndexOf('\\');
        // Cut off at latest possible point
        int pos = (winSep > unixSep ? winSep : unixSep);
        if (pos != -1)  {
            // Any sort of path separator found...
            fileName = fileName.substring(pos + 1);
        }
        //替换上传文件名字的特殊字符
        fileName = fileName.replace("=","").replace(",","").replace("&","");
        return fileName;
    }
}