SpringBoot_構建jar包之後文件讀取路徑問題

最近在寫文件管理的模塊,使用了springboot整合fdfs,在本地測試沒問題之後,發佈jar時出現了問題:

static {
        try {
            String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();
            ClientGlobal.init(filePath);
        } catch (Exception e) {
            e.getStackTrace();
        }
    }

上面的代碼在本地idea運行是沒有問題的,但是到了打包成jar包並且在命令行使用java -jar XXX.jar運行的時候,會讀取不到文件的正確路徑。建議改爲以下方式:

    private int initFastDFS(String confFile){
        if (confFile.isEmpty()){
            return ErrNo.ERROR_FILEMGR_INIT;
        }

        String filePath = getFilePath(confFile);
        try {
            ClientGlobal.init(filePath);
        } catch (Exception e){
            e.printStackTrace();
            return ErrNo.ERROR_FILEMGR_INIT;
        }

        return ErrNo.ERROR_SUCCESS;
    }

    private String getFilePath(String file) {
        String userDir = System.getProperty("user.dir");
        String filePath = userDir + "/" + file;
        return filePath;
    }
    initFastDFS("fdfs_client.conf");

這樣的話也存在問題,在C:\user\906760的命令行進行運行時候,需要將fdfs_client.conf對應的文件放在C:\user\906760目錄下,才能正確讀取。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章