I want to encode a file it may be image or any pdf and send it to server. Which type of Encoding and decoding I have to follow. (Both server and client is in our company. we can write logic in both place). UTF-8 Encoding is by default supported in java. and to use Base-64 encoding I have to import external jar. for simple texts both the ways are working fine. I am using tcp socket programming.
Using UTF-8 Encoding
String str = "This is my Sample application";
String urlEncodedData = URLEncoder.encode(str, "UTF-8"); // Encoding with UTF-8
System.out.println("..after URL Encodingencoding..."+urlEncodedData );
String retrievedData = URLDecoder.decode(urlEncodedData , "UTF-8");// Decoding with UTF-8
System.out.println("..after decoding..."+retrievedData );
Using Base-64 (Using commons.codec jar of apache
byte[] b =Base64.encodeBase64(str.getBytes()); //Encoding base 64
Base64.decodeBase64(b); // Decoding with Base 64