admin
2021-10-13 052e1d5c47c4e536fde79074d53b0481c7d4f9b6
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
package org.yeshi.utils;
 
import java.io.*;
 
import javax.servlet.http.HttpServletRequest;
 
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
 
public class FileUtil {
 
    @SuppressWarnings("deprecation")
    public static String getRealPath(HttpServletRequest request, String folderName) {
        String f = request.getRealPath(folderName);
        if (!new File(f).exists())
            new File(f).mkdirs();
        return f;
    }
 
    public static String getFileMD5(File file) throws IOException {
        FileInputStream fis = new FileInputStream(file);
        String md5 = DigestUtils.md5Hex(IOUtils.toByteArray(fis));
        IOUtils.closeQuietly(fis);
        return md5;
    }
 
    public static void deleteFileDir(File path) {
        if (!path.exists())
            return;
        if (path.isFile()) {
            path.delete();
            return;
        }
        File[] files = path.listFiles();
        for (int i = 0; i < files.length; i++) {
            deleteFileDir(files[i]);
        }
        path.delete();
    }
 
    /**
     * 将输入流存为本地文件
     *
     * @param inputStream
     * @param path
     * @return
     * @throws IOException
     */
    public static String saveAsFile(InputStream inputStream, String path) throws IOException {
        if (!new File(path).exists()) {
            new File(path).createNewFile();
        }
 
        FileOutputStream fileOut = new FileOutputStream(new File(path));
        byte[] buf = new byte[1024 * 8];
        while (true) {
            int read = 0;
            if (inputStream != null) {
                read = inputStream.read(buf);
            }
            if (read == -1) {
                break;
            }
            fileOut.write(buf, 0, read);
        }
        fileOut.close();
        return path;
    }
 
    /**
     * 获取缓存文件夹目录
     *
     * @return
     */
    public static String getCacheDir() {
        String os = System.getProperty("os.name");
        if (os.toLowerCase().startsWith("win")) {
            File f = new File("D:/cache");
            if (!f.exists())
                f.mkdirs();
            return f.getPath();
        } else {
            File f = new File("/usr/local/cache");
            if (!f.exists())
                f.mkdirs();
            return f.getPath();
        }
    }
 
 
    /**
     * 将输入流存为本地文件
     *
     * @param b
     * @param path
     * @return
     * @throws IOException
     */
    public static String saveAsFileByte(byte[] b, String path) throws IOException {
        if (!new File(path).exists()) {
            new File(path).createNewFile();
        }
 
        FileOutputStream fileOut = new FileOutputStream(path);
        fileOut.write(b);
        fileOut.flush();
        fileOut.close();
        return path;
    }
 
 
    /**
     * 复制inputStream
     * @param input
     * @return
     */
    public static ByteArrayOutputStream cloneInputStream(InputStream input) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = input.read(buffer)) > -1) {
                baos.write(buffer, 0, len);
            }
            baos.flush();
            return baos;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}