I want to display the image received by the client and show that image on the server.. but I have kept the image part to be done later and initially just receive the inputstream and print it.
The problem is it is accepting the client request but doesnot print the decodedstring i.e decodedString = Base64.decodeBase64(base64Code);
Here's the code for the server
import java.net.*;
import java.io.*;
import org.apache.commons.codec.binary.Base64;
import java.awt.*;
import javax.swing.*;
public class server {
private static DataInputStream dataInputStream;
private static DataOutputStream dataOutputStream;
public static void main(String[] args) throws IOException {
// create socket
ServerSocket servsock = new ServerSocket(14789);
Socket sock = servsock.accept();
dataInputStream = new DataInputStream(sock.getInputStream());
dataOutputStream = new DataOutputStream(sock.getOutputStream());
System.out.println("Accepted connection : " + sock);
String base64Code = dataInputStream.readUTF();
byte[] decodedString = null;
decodedString = Base64.decodeBase64(base64Code);
System.out.println("Image Successfully Manipulated!" + decodedString);
if (dataOutputStream != null) {
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataInputStream != null) {
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
sock.close();
}
public static String encodeImage(byte[] imageByteArray) {
return Base64.encodeBase64String(imageByteArray);
}
}
Any help will be highly appreciated.
You cant just print array of bytes, the code
System.out.println("Image Successfully Manipulated!" + decodedString);
print something like
Image Successfully Manipulated![B@1cd2e5f
If you want to print byte array as String you should convert byte array to hex String, for example like this:
public static String byteArrayToHexString(byte[] b)
{
StringBuffer sb = new StringBuffer(b.length * 2);
for (int i = 0; i < b.length; i++)
{
int v = b[i] & 0xff;
if (v < 16)
{
sb.append('0');
}
sb.append(Integer.toHexString(v));
}
return sb.toString().toUpperCase();
}