October 26, 2014

How to connect remote machine in Unix

Below command is useful to connect remote machine or server in Unix.

SSH <user>@<host name>

Example:

SSH anand@mymachine

October 16, 2014

URL Encoding in Java

Whenever we are passing data to any url or web services, some characters of the data might be un recognised. Hence, it is always better to encode data before sending to URLs or web services, which we call as URL encoding. We have pre defined apis in java to do this encoding.

Below is an example to show how to encode a string in java.

String str="Hello World";
java.net.URLEncoder..encode(str, "UTF-8");

The output for this code would be:

Hello+World.

Convert an image to Base64

This post is to show how to generate base64 string to an image file.

Below is the method which will return the base64 string to the given image file.
The input for this method is the full path of the image.

    public String getBase64ForFile(String inFile) throws Exception {
        String base64 = "";
        System.out.println("inFile: " + inFile);
        URL url = new URL(inFile);
        InputStream fin = url.openStream();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int next = 0;
        while ((next = fin.read()) != -1) {
            bos.write(next);
        }
        bos.flush();
        base64 = "" + org.kobjects.base64.Base64.encode(bos.toByteArray());
        System.out.println("Base64: " + base64);

        bos.close();
        fin.close();
        return base64;
    }