Java Code Examples for cn.hutool.core.io.FileUtil#readString()

The following examples show how to use cn.hutool.core.io.FileUtil#readString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SshInstallAgentController.java    From Jpom with MIT License 6 votes vote down vote up
private String getAuthorize(SshModel sshModel, NodeModel nodeModel, String path) {
    File saveFile = null;
    try {
        String tempFilePath = ServerConfigBean.getInstance().getUserTempPath().getAbsolutePath();
        //  获取远程的授权信息
        String normalize = FileUtil.normalize(StrUtil.format("{}/{}/{}", path, ConfigBean.DATA, ConfigBean.AUTHORIZE));
        saveFile = FileUtil.file(tempFilePath, IdUtil.fastSimpleUUID() + ConfigBean.AUTHORIZE);
        sshService.download(sshModel, normalize, saveFile);
        //
        String json = FileUtil.readString(saveFile, CharsetUtil.CHARSET_UTF_8);
        AgentAutoUser autoUser = JSONObject.parseObject(json, AgentAutoUser.class);
        nodeModel.setLoginPwd(autoUser.getAgentPwd());
        nodeModel.setLoginName(autoUser.getAgentName());
    } catch (Exception e) {
        DefaultSystemLog.getLog().error("拉取授权信息失败", e);
        return JsonMessage.getString(500, "获取授权信息失败", e);
    } finally {
        FileUtil.del(saveFile);
    }
    return null;
}
 
Example 2
Source File: JsonFileUtil.java    From Jpom with MIT License 6 votes vote down vote up
/**
 * 读取json 文件,同步
 *
 * @param path 路径
 * @return JSON
 * @throws FileNotFoundException 文件异常
 */
public static JSON readJson(String path) throws FileNotFoundException {
    File file = new File(path);
    if (!file.exists()) {
        throw new FileNotFoundException("没有找到对应配置文件:" + path);
    }
    READ_LOCK.lock();
    // 防止多线程操作文件异常
    try {
        String json = FileUtil.readString(file, CharsetUtil.UTF_8);
        if (StrUtil.isEmpty(json)) {
            return new JSONObject();
        }
        try {
            return (JSON) JSON.parse(json);
        } catch (Exception e) {
            throw new JpomRuntimeException("数据文件内容错误,请检查文件是否被非法修改:" + path, e);
        }
    } finally {
        READ_LOCK.unlock();
    }
}
 
Example 3
Source File: MybatisUtil.java    From MooTool with MIT License 5 votes vote down vote up
/**
 * 初始化数据库文件
 */
public static void initDbFile() throws SQLException {
    File configHomeDir = new File(SystemUtil.configHome);
    if (!configHomeDir.exists()) {
        configHomeDir.mkdirs();
    }
    // 不存在db文件时会自动创建一个
    String sql = FileUtil.readString(MybatisUtil.class.getResource("/db_init.sql"), CharsetUtil.UTF_8);
    executeSql(sql);
    needInit = true;
}
 
Example 4
Source File: ScriptController.java    From Jpom with MIT License 5 votes vote down vote up
@RequestMapping(value = "upload.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public String upload() throws IOException {
    MultipartFileBuilder multipartFileBuilder = createMultipart()
            .addFieldName("file").setFileExt("bat", "sh");
    multipartFileBuilder.setSavePath(AgentConfigBean.getInstance().getTempPathName());
    multipartFileBuilder.setUseOriginalFilename(true);
    String path = multipartFileBuilder.save();
    File file = FileUtil.file(path);
    String context = FileUtil.readString(path, JpomApplication.getCharset());
    if (StrUtil.isEmpty(context)) {
        return JsonMessage.getString(405, "脚本内容为空");
    }
    String id = file.getName();
    ScriptModel eModel = scriptServer.getItem(id);
    if (eModel != null) {
        return JsonMessage.getString(405, "对应脚本模板已经存在啦");
    }
    eModel = new ScriptModel();
    eModel.setId(id);
    eModel.setName(id);
    eModel.setContext(context);
    file = eModel.getFile(true);
    if (file.exists() || file.isDirectory()) {
        return JsonMessage.getString(405, "当地id路径文件已经存在来,请修改");
    }
    scriptServer.addItem(eModel);
    return JsonMessage.getString(200, "导入成功");
}
 
Example 5
Source File: ScriptModel.java    From Jpom with MIT License 5 votes vote down vote up
public void readFileContext() {
    File file = getFile(true);
    if (FileUtil.exist(file)) {
        //
        String context = FileUtil.readString(file, JpomApplication.getCharset());
        setContext(context);
    }
}
 
Example 6
Source File: AutoImportLocalNode.java    From Jpom with MIT License 5 votes vote down vote up
private static void findPid(String pid) {
    File file = ConfigBean.getInstance().getApplicationJpomInfo(Type.Agent);
    if (!file.exists() || file.isDirectory()) {
        return;
    }
    // 比较进程id
    String json = FileUtil.readString(file, CharsetUtil.CHARSET_UTF_8);
    JpomManifest jpomManifest = JSONObject.parseObject(json, JpomManifest.class);
    if (!pid.equals(String.valueOf(jpomManifest.getPid()))) {
        return;
    }
    // 判断自动授权文件是否存在
    String path = ConfigBean.getInstance().getAgentAutoAuthorizeFile(jpomManifest.getDataPath());
    if (!FileUtil.exist(path)) {
        return;
    }
    json = FileUtil.readString(path, CharsetUtil.CHARSET_UTF_8);
    AgentAutoUser autoUser = JSONObject.parseObject(json, AgentAutoUser.class);
    // 判断授权信息
    //
    NodeModel nodeModel = new NodeModel();
    nodeModel.setUrl(StrUtil.format("127.0.0.1:{}", jpomManifest.getPort()));
    nodeModel.setName("本机");
    nodeModel.setId("localhost");
    //
    nodeModel.setLoginPwd(autoUser.getAgentPwd());
    nodeModel.setLoginName(autoUser.getAgentName());
    //
    nodeModel.setOpenStatus(true);
    nodeService.addItem(nodeModel);
    DefaultSystemLog.getLog().info("自动添加本机节点成功:" + nodeModel.getId());
}
 
Example 7
Source File: MybatisUtil.java    From WePush with MIT License 5 votes vote down vote up
/**
 * 初始化数据库文件
 */
public static void initDbFile() throws SQLException {
    File configHomeDir = new File(SystemUtil.configHome);
    if (!configHomeDir.exists()) {
        configHomeDir.mkdirs();
    }
    // 不存在db文件时会自动创建一个
    String sql = FileUtil.readString(MainWindow.class.getResource("/db_init.sql"), CharsetUtil.UTF_8);
    executeSql(sql);
    needInit = true;
}