MainController.java 1.22 KB
package com.viontech.domain;

import cn.hutool.core.io.FileUtil;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

@RestController
public class MainController {
    @Resource
    private StringEncryptor stringEncryptor;

    @RequestMapping(value = "/encrypt/{value}", method = RequestMethod.GET)
    @ResponseBody
    public String encrypt(@PathVariable("value") String value) throws IOException {
        String path;
        String encrypt = "ENC(" + this.stringEncryptor.encrypt(value) + ")\n";
        try {
            path = ResourceUtils.getFile("").getAbsolutePath();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            path = System.getProperty("java.io.tmpdir");
        }

        File file = new File(path + "\\result.txt");
        // 明文和加密后的内容写入文件
        FileUtil.writeLines(Arrays.asList(value, encrypt), file, StandardCharsets.UTF_8, true);
        return "文件写入成功";
    }
}