Thursday, 20 September 2012

How to access a .properties file on remote server using Java

One of the simplest ways to access a .properties file on remote server is by sending a HTTP request. Similarly any other type of file can also be accessed by using the appropriate API
It can be done in the following way...


Properties prop = new Properties();

    try {
               //load a properties file
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new  HttpPost("http://hostname/xxx.properties");
    HttpResponse response =   httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    InputStream is = entity.getContent();
    prop.load(is);

    //get the property value and print it out
    System.out.println(prop.getProperty("url"));
   
    } catch (IOException ex) {
    ex.printStackTrace();
        }

    }

No comments:

Post a Comment

Got to say something?...Leave your comments here