读取.properties文件中的路径

时间:2016-11-03 07:09:53

标签: java properties path

我试图把我所有的网址和& .properties文件中的路径。因此,如果我要改变它们会更容易。所以这里是我的path.properties(它在src \ main \ resources \ META-INF \ spring \ path.properties中):

path.clientIdentify=C:\\Palms\\client-identify-bin\\dll
path.clientEnroll=C:\\Palms\\client-enroll-bin\\dll
path.pvInfoIni=C://Palms//PV//PVInfo.ini
path.pvEnrollIni=C://Palms//PV//PVEnroll.ini

我试图调用控制器中的路径,所以这就是我所做的:

@Controller
@RequestMapping("/call")
public class PalmsController {

    @RequestMapping(value = "/palmsIdentify")
    public ResponseEntity<String> palmusIdentify() throws IOException, InterruptedException {

        Properties properties = new Properties();
        try (InputStream is = new FileInputStream("classpath*:META-INF/spring/*.properties")) {
            properties.load(is);
        }

        HttpHeaders headers = new HttpHeaders();
        ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "StartSample.bat");
        builder.directory(new File("path.clientIdentify"));
        Process process = builder.start();
        BufferedReader r = new BufferedReader(new InputStreamReader(process.getInputStream()));
        r.close();
        process.waitFor();

        Properties p = new Properties();
        try {
            p.load(new FileInputStream("path.pvInfoIni"));
            String pvidNO1 = p.getProperty("PVIDNO");
            String pvidNo2 = p.getProperty("PVIDNo");
            String authentication = p.getProperty("Authentication");

            // Convert to JSON
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("pvId", pvidNO1);
            jsonObject.put("PVIDNo", pvidNo2);
            jsonObject.put("is_Authenticated", authentication);

            return new ResponseEntity<String>(jsonObject.toString(),headers ,HttpStatus.OK);

        }
        catch (Exception e) {
            return new ResponseEntity<String>("{\"ERROR\":" + e.getMessage() + "\"}", headers,
                    HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping(value = "/palmusEnroll")
    public ResponseEntity<String> palmusEnroll() throws IOException, InterruptedException {
        Properties properties = new Properties();
        try (InputStream is = new FileInputStream("classpath*:META-INF/spring/*.properties")) {
            properties.load(is);
        }

        HttpHeaders headers = new HttpHeaders();
        ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "StartSample.bat");
        builder.directory(new File("path.clientEnroll"));
        Process process = builder.start();
        BufferedReader r = new BufferedReader(new InputStreamReader(process.getInputStream()));
        r.close();
        process.waitFor();

        // PARSING
        Properties p = new Properties();
        try {
            p.load(new FileInputStream("path.pvEnrollIni"));
            String pvidNO1 = p.getProperty("PVIDNO");
            String palmusId = p.getProperty("PALMUS_ID");


            // Convert to JSON
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("pvId", pvidNO1);
            jsonObject.put("palmusId", palmusId);
            return new ResponseEntity<String>(jsonObject.toString(),headers ,HttpStatus.OK);
        } 
        catch (Exception e) {
            return new ResponseEntity<String>("{\"ERROR\":" + e.getMessage() + "\"}", headers,
                    HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

}

但它似乎并没有称之为路径。或者也许我做得不对?对不起新手在这里,我希望有人可以帮助我。谢谢。

1 个答案:

答案 0 :(得分:0)

您没有从属性文件中获取值。如果加载了属性对象,则可以通过properties.getProperty("key")从属性文件中获取值。

使用以下代码段加载资源属性文件:

  properties.load(getClass().getClassLoader().getResourceAsStream("META-INF/spring/path.properties"));

它应该取代:

try (InputStream is = new FileInputStream("classpath*:META-INF/spring/*.properties")) {
    properties.load(is);
}

然后你可以得到这样的路径:

properties.getProperty("path.clientIdentify");