puzhibing
2023-11-28 7b726d5ff439e7b8bb66369ecc5881e370286bc8
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
package com.stylefeng.guns.modular.system.controller;
 
import com.baidu.ueditor.ActionEnter;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
 
/**
 * 百度富文本控制器
 */
@Controller
public class UEditorController {
 
 
 
    @RequestMapping("/config.json")
    public void ueditorConfig(HttpServletRequest request, HttpServletResponse response){
        String rootPath = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "static/ueditor1_4_3_3/jsp";
        String property = System.getProperty("os.name");
        try {
            rootPath = URLDecoder.decode(rootPath, "UTF-8");
            if(property.indexOf("Windows") != -1){
                rootPath = rootPath.substring(1);
            }
            response.setContentType("application/json");
            PrintWriter out = response.getWriter();
            String exec = new ActionEnter(request, rootPath).exec();
//            StringBuilder builder = new StringBuilder();
//            InputStreamReader reader = new InputStreamReader(new FileInputStream(rootPath), "UTF-8");
//            BufferedReader bfReader = new BufferedReader(reader);
//            String tmpContent = null;
//            while((tmpContent = bfReader.readLine()) != null) {
//                builder.append(tmpContent);
//            }
//            bfReader.close();
//            String s = builder.toString().replaceAll("/\\*[\\s\\S]*?\\*/", "");
 
            out.write(exec);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
 
 
    @RequestMapping("/ueditor/uploadFileData")
    @ResponseBody
    public Map<String,String> uploadFileData(@RequestParam("upfile") MultipartFile upfile, HttpServletRequest request) throws IOException {
        //文件原名称
        String fileName = upfile.getOriginalFilename();
        // 保存文件的新名字
        String nowName = fileName.substring(upfile.getOriginalFilename().lastIndexOf("."));
//        String s = uploadUtil.uploadFile(2, "file", upfile);
        String s = "";
        //返回结果信息(UEditor官方要求这个json格式)
        Map<String,String> map = new HashMap<String,String >();
        //是否上传成功
        map.put("state", "SUCCESS");
        //现在文件名称
        map.put("title", nowName);
        //文件原名称
        map.put("original", fileName);
        //文件类型 .+后缀名
        map.put("type", fileName.substring(upfile.getOriginalFilename().lastIndexOf(".")));
        //文件路径
        map.put("url", s);
        //文件大小(字节数)
        map.put("size", upfile.getSize()+"");
        return map;
    }
 
 
    @RequestMapping("/ueditor/uploadImageData")
    @ResponseBody
    public Map<String,String> uploadImage(@RequestParam("upfile") MultipartFile upfile, HttpServletRequest request) throws IOException {
        //文件原名称
        String fileName = upfile.getOriginalFilename();
        // 保存文件的新名字
        String nowName = UUID.randomUUID()+fileName.substring(upfile.getOriginalFilename().lastIndexOf("."));
//        String s = uploadUtil.uploadFile(1, "img", upfile);
        String s = "";
        //返回结果信息(UEditor官方要求这个json格式)
        Map<String,String> map = new HashMap<String,String >();
        //是否上传成功
        map.put("state", "SUCCESS");
        //现在文件名称
        map.put("title", nowName);
        //文件原名称
        map.put("original", fileName);
        //文件类型 .+后缀名
        map.put("type", fileName.substring(upfile.getOriginalFilename().lastIndexOf(".")));
        //文件路径
        map.put("url", s);
        //文件大小(字节数)
        map.put("size", upfile.getSize()+"");
        return map;
    }
}