Lius
2025-02-18 3423bb9ee5b25d270a00763b69ed73970d790f63
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
package org.jeecg.modules.mdc.util;
 
import cn.hutool.core.date.DateUtil;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.util.Date;
 
/**
 * Created by YangBin on 2017/9/15.
 */
public class FileUtils {
    private static final long MAX_FILE_SIZE = 10*1024*1024;
    /**
     * 获取服务器临时路径
     *
     * @param request
     * @return     */
    public static String serverTempPath(HttpServletRequest request) {
        String fsep = System.getProperty("file.separator");
        String path = request.getSession().getServletContext().getRealPath("");
        if (path.lastIndexOf(fsep) == path.length() - 1) {
            path += "upload" + fsep +"voucher"+ fsep ;
        } else {
            path += fsep + "upload" + fsep +"voucher"+ fsep ;
        }
        return path;
    }
 
    public static String serverTempPathIntactReg(HttpServletRequest request) {
        String fsep = System.getProperty("file.separator");
        String path = request.getSession().getServletContext().getRealPath("");
        if (path.lastIndexOf(fsep) == path.length() - 1) {
            path += "upload" + fsep +"log"+ fsep +"error"+ fsep ;
        } else {
            path += fsep + "upload" + fsep +"log"+ fsep  +"error"+ fsep;
        }
        return path;
    }
 
    public static String serverTempPathProcess(HttpServletRequest request) {
        String fsep = System.getProperty("file.separator");
        String path = request.getSession().getServletContext().getRealPath("");
        if (path.lastIndexOf(fsep) == path.length() - 1) {
            path += "upload" + fsep +"process"+ fsep ;
        } else {
            path += fsep + "upload" + fsep +"process"+ fsep ;
        }
        return path;
    }
 
    public static String changeFileFormatKb(String flow) {
        BigDecimal flows = new BigDecimal(flow);
        if (flows.compareTo(new BigDecimal(0)) > 0 && flows.compareTo(new BigDecimal(1024)) < 0) {//小于1M
            return flows.toString() + "B";
        } else if(flows.compareTo(new BigDecimal(1024)) >= 0){
            BigDecimal result = flows.divide(new BigDecimal(1024),2,BigDecimal.ROUND_HALF_UP);
            return  result.toString() + "KB";
        } else {
            return "0";
        }
    }
 
 
    /**
     * 得到项目根目录下的绝对路径(磁盘的物理路径)
     * @param request
     * @param newPath
     * @return
     */
    public static String getFilePath(HttpServletRequest request, String newPath) {
        String fsep = System.getProperty("file.separator");
        String path = request.getSession().getServletContext().getRealPath("/upload");
        path += fsep + newPath;
        return path;
    }
 
    /**
     * 得到项目根目录下的相对路径 (相对于项目为根路径)
     * @param newPath
     * @return
     */
    public static String getRelativePath(String newPath) {
        return "/upload/" + newPath;
    }
 
 
    /**
     * 方法一:使用 FileWriter 写文件
     * @param filepath 文件目录
     * @param content  待写入内容
     * @throws IOException
     */
    public static void fileWriterSql(String filepath, String content) throws IOException {
        OutputStreamWriter outputStreamWriter = null;
        try {
            File file = new File(filepath);
            if (!file.exists()){
                file.createNewFile();
            }
            FileOutputStream outputStream = new FileOutputStream(file);
            if (outputStream != null){
                outputStreamWriter = new OutputStreamWriter(outputStream, "utf-8");
                outputStreamWriter.write(content);
                outputStreamWriter.flush();
            }
        }
        catch (IOException e) {
            e.getMessage();
        }
        finally {
            try {
                if (outputStreamWriter != null){
                    outputStreamWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
 
        /*
        File file=new File(filepath);
        Writer writer = new OutputStreamWriter(new FileOutputStream(file.getAbsoluteFile()), "UTF-8");
        writer.append(content);
         */
    }
 
    /**
     * 文件大小智能转换
     * 会将文件大小转换为最大满足单位
     * @param size(文件大小,单位为B)
     * @return 文件大小
     */
    public static String formatFileSize(Long size) {
        String sizeName = null;
        if(1024*1024 > size && size >= 1024 ) {
            sizeName = String.format("%.2f",size.doubleValue()/1024) + "KB";
        }else if(1024*1024*1024 > size && size >= 1024*1024 ) {
            sizeName = String.format("%.2f",size.doubleValue()/(1024*1024)) + "MB";
        }else if(size >= 1024*1024*1024 ) {
            sizeName = String.format("%.2f",size.doubleValue()/(1024*1024*1024)) + "GB";
        }else {
            sizeName = size.toString() + "B";
        }
        return sizeName;
    }
 
    /**
     * 上传文件工具类
     * @param userPic
     * @param path
     * @param fileName
     * @return
     */
    public static boolean uploadFile(MultipartFile userPic, String path, String fileName) {
        Long fileSize = userPic.getSize();
        if(fileSize > MAX_FILE_SIZE) {
            return false;
        }
        File targetFile = new File(path, fileName);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }
        try {
            userPic.transferTo(targetFile);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
 
//    /**
//     * 重命名文件名 加入时间戳
//     * @param fileName
//     * @return
//     */
//    public static String newFileName(String fileName) {
//        String suffix = fileName.substring(fileName.lastIndexOf('.'));
//        String dateStr = "_"+ DateUtil.format(new Date(), DateUtil.STR_DATE_TIME_FULL);
//        String nameFile = fileName.substring(0, fileName.indexOf("."));
//        //新文件名
//        return nameFile +  dateStr + suffix;
//    }
}