路泰机电科技体检——数据平台后端
zhaowenxuan
2024-07-01 1eafa4de4abae5aafe470d1f301f3060a83ab0f8
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
package com.example.controller;
 
import com.example.utils.DictionaryUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.io.*;
 
/**
 * @Company: 西安路泰科技有限公司
 * @Author: zhaowenxuan
 * @Date: 2024/6/3 15:25
 */
@Slf4j
@RestController
public class TestController {
    private static final String LOG_PATH = "src/main/resources/log.log";
    @Autowired
    private DictionaryUtil dictionaryUtil;
    @GetMapping("exec")
    public String exce(){
        // 将文件内容设置为空
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(LOG_PATH);
            fileWriter.write("");
            fileWriter.close();
        } catch (IOException ignored) {}
        new Thread(()->{
            try {
                dictionaryUtil.exec1();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
        return "已提交执行";
    }
 
    @GetMapping("/show")
    public String showLog() {
        StringBuilder logContent = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new FileReader(LOG_PATH))) {
            String line;
            while ((line = reader.readLine()) != null) {
                logContent.append(line).append("<br>");
            }
            return logContent.toString();
        } catch (IOException e) {
            log.error("Error reading log file", e);
            return e.getMessage();
        }
    }
 
    @GetMapping("/clear")
    public String cleanLog() {
        try {
            // 将文件内容设置为空
            FileWriter fileWriter = new FileWriter(LOG_PATH);
            fileWriter.write("");
            fileWriter.close();
            return "Log content cleared successfully.";
        } catch (IOException e) {
            e.printStackTrace();
            return "Failed to clear log content.";
        }
    }
}