2016-11-03 3 views
0

Ich versuche, alle meine URLs & Pfade in .properties-Datei zu setzen. Wenn ich sie also ändern würde, wäre es einfacher. Also hier ist mein path.properties (es ist in src \ main \ resources \ META-INF \ spring \ path.properties):Lesen von Pfaden in Datei .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 

Und ich habe versucht, die Wege in meinem Controller zu nennen, so ist dies, was ich did:

@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); 
     } 

    } 

} 

Aber es scheint, als würde es nicht die Pfade aufrufen. Oder vielleicht mache ich es nicht richtig? Sorry Neuling hier, ich hoffe jemand kann mir helfen. Vielen Dank.

Antwort

0

Sie erhalten den Wert nicht aus der Eigenschaftendatei. Wenn das Properties-Objekt geladen ist, können Sie den Wert aus der Eigenschaftendatei unter abrufen.


Legen Sie die Ressourcen-Properties-Datei den folgenden Code-Schnipsel mit:

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

Und es sollte ersetzt werden:

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

Und dann können Sie die Pfade wie diese:

properties.getProperty("path.clientIdentify"); 
+0

Funktionierte immer noch nicht Sir. –

+0

Können Sie debuggen, wenn Ihr Code sogar die Eigenschaftendatei lädt? – kevto

+0

Ich habe die Fehlerressource nicht gefunden. Daher wird die Eigenschaftendatei nicht geladen. Warum das? –

Verwandte Themen