懒羊羊
2023-11-14 8286c62256f23bc2367a6729c0f46f84215e380b
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
package cn.stylefeng.guns.sys.core.util;
 
import cn.hutool.core.io.FileUtil;
import cn.stylefeng.guns.base.consts.ConstantsContext;
import cn.stylefeng.guns.sys.core.listener.ConfigListener;
import cn.stylefeng.guns.sys.modular.system.model.UeditorFileResult;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
 
import static cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum.*;
 
/**
 * 百度富文本的工具类
 *
 * @author fengshuonan
 * @Date 2019/8/26 15:27
 */
@Slf4j
public class UeditorUtil {
 
    /**
     * ue上传文件逻辑
     *
     * @author fengshuonan
     * @Date 2019-08-27 12:54
     */
    public static UeditorFileResult uploadFile(MultipartFile upfile, FileType fileType) {
        if (upfile.isEmpty()) {
            throw new ServiceException(UE_FILE_NULL_ERROR);
        }
 
        // 获取文件名,后缀名
        String oldFileName = upfile.getOriginalFilename();
        String suffixName = ToolUtil.getFileSuffix(oldFileName);
 
        // 重新命名图片
        String newFileName = IdWorker.getIdStr() + "." + suffixName;
 
        UeditorFileResult ueditorFileResult = new UeditorFileResult();
        String path = null;
 
        // 如果是上传图片
        if (fileType.equals(FileType.IMG)) {
 
            ueditorFileResult.setUrl(UeditorUtil.getImageRelativeUrl(newFileName));
            ueditorFileResult.setTitle(newFileName);
            ueditorFileResult.setOriginal(newFileName);
 
            //文件用原始文件
            path = ConstantsContext.getFileUploadPath() + newFileName;
 
 
        } else if (fileType.equals(FileType.FILE)) {
 
            // 如果是上传文件
            ueditorFileResult.setUrl(UeditorUtil.getFileRelativeUrl(newFileName) + "/" + oldFileName);
            ueditorFileResult.setTitle(oldFileName);
            ueditorFileResult.setOriginal(oldFileName);
 
            //文件用原始文件
            path = ConstantsContext.getFileUploadPath() + newFileName;
 
        } else {
 
            // 如果是上传视频
            ueditorFileResult.setUrl(UeditorUtil.getVideoRelativeUrl(newFileName));
            ueditorFileResult.setTitle(newFileName);
            ueditorFileResult.setOriginal(newFileName);
 
            //文件用原始文件
            path = ConstantsContext.getFileUploadPath() + newFileName;
 
        }
 
        try {
 
            File dest = new File(path);
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
 
            upfile.transferTo(dest);
            return ueditorFileResult;
        } catch (IOException e) {
            log.error("保存ue的上传文件出错!", e);
            throw new ServiceException(UE_FILE_SAVE_ERROR);
        }
    }
 
    /**
     * 读取文件
     *
     * @author fengshuonan
     * @Date 2019-08-27 13:02
     */
    public static void readFile(String fileName, HttpServletResponse response, FileType fileType, String orginalName) {
 
        if (ToolUtil.isEmpty(fileName)) {
            throw new ServiceException(UE_FILE_NULL_ERROR);
        }
 
        //获取文件路径
        String path = ConstantsContext.getFileUploadPath() + fileName;
        File file = new File(path);
 
        //文件不存在或者不可读
        if (!file.exists() || !file.canRead()) {
            throw new ServiceException(UE_FILE_NULL_ERROR);
        }
 
        //读取文件
        byte[] bytes = null;
 
        //设置响应的类型
        if (fileType.equals(FileType.IMG)) {
 
            response.setContentType("image/png");
            bytes = FileUtil.readBytes(file);
 
        } else if (fileType.equals(FileType.FILE)) {
 
            response.setContentType("multipart/form-data;charset=utf-8");
 
            //判断文件是否已经被重命名
            String newFilePath = ConstantsContext.getFileUploadPath() + orginalName;
            File newFile = new File(newFilePath);
            if (!newFile.exists()) {
                newFile = UeditorUtil.reName(file, orginalName);
            }
            bytes = FileUtil.readBytes(newFile);
 
        } else {
 
            response.setContentType("video/x-sgi-movie");
            bytes = FileUtil.readBytes(file);
 
        }
 
        try {
            OutputStream stream = response.getOutputStream();
            stream.write(bytes);
        } catch (IOException e) {
            log.error("读取文件错误!", e);
            throw new ServiceException(UE_FILE_READ_ERROR);
        }
    }
 
    /**
     * 获取图片相对路径
     *
     * @author fengshuonan
     * @Date 2019/8/26 15:21
     */
    private static String getImageRelativeUrl(String imageName) {
        String contextPath = ConfigListener.getConf().get("contextPath");
        return contextPath + "/ueditor/images/" + imageName;
    }
 
    /**
     * 获取文件相对路径
     *
     * @author fengshuonan
     * @Date 2019/8/26 15:22
     */
    private static String getFileRelativeUrl(String imageName) {
        String contextPath = ConfigListener.getConf().get("contextPath");
        return contextPath + "/ueditor/file/" + imageName;
    }
 
    /**
     * 获取视频相对路径
     *
     * @author fengshuonan
     * @Date 2019/8/26 15:22
     */
    private static String getVideoRelativeUrl(String imageName) {
        String contextPath = ConfigListener.getConf().get("contextPath");
        return contextPath + "/ueditor/video/" + imageName;
    }
 
    /**
     * 文件重命名
     *
     * @author fengshuonan
     * @Date 2019/8/26 15:23
     */
    private static File reName(File file, String newName) {
        if (file.exists()) {
 
            //创建新名字的抽象文件
            File newfile = new File(file.getParent() + File.separator + newName);
 
            if (file.renameTo(newfile)) {
                log.info("重命名成功!");
                return newfile;
            } else {
                log.info("重命名失败!新文件名已存在!");
                return file;
            }
        } else {
            log.info("重命名文件不存在!");
            return file;
        }
    }
 
    /**
     * 文件类型
     *
     * @author fengshuonan
     * @Date 2019-08-27 12:52
     */
    public enum FileType {
 
        /**
         * 图片类型
         */
        IMG,
 
        /**
         * 文件类型
         */
        FILE,
 
        /**
         * 视频类型
         */
        VIDEO
    }
 
}