In Java with ServerSocket
and Socket
I can transfer image file using the below code in a Local Network:
Send
public class Send {
public static void main(String[] args) throws Exception {
Socket socket = new Socket(serverIP, serverPORT);
OutputStream outputStream = socket.getOutputStream();
BufferedImage image = ImageIO.read(new File("test.jpg"));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", byteArrayOutputStream);
byte[] size = ByteBuffer.allocate(4).putInt(byteArrayOutputStream.size()).array();
outputStream.write(size);
outputStream.write(byteArrayOutputStream.toByteArray());
outputStream.flush();
socket.close();
}
}
Receive
public class Receive {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(serverPORT);
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
byte[] sizeAr = new byte[4];
inputStream.read(sizeAr);
int size = ByteBuffer.wrap(sizeAr).asIntBuffer().get();
byte[] imageAr = new byte[size];
inputStream.read(imageAr);
BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageAr));
ImageIO.write(image, "jpg", new File("test2.jpg"));
serverSocket.close();
}
}
How can I do it in Netty
using Socket?
My Handler
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object o) throws Exception {
Channel currentChannel = ctx.channel();
System.out.println(TAG + "MESSAGE FROM SERVER - " + currentChannel.remoteAddress() + " - " + o);
List<Object> msg = new ArrayList<>();
msg.addAll((Collection<? extends Object>) o);
/*If message in index 0 is Equal to IMAGE then I need to send an Image File*/
if(msg.get(0).equals("IMAGE")){
/*
NO IDEA on how can I send it on Netty.
I'm not sure if this will work or this is how should I do it.
*/
BufferedImage image = ImageIO.read(new File("test.jpg"));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", byteArrayOutputStream);
byte[] size = ByteBuffer.allocate(4).putInt(byteArrayOutputStream.size()).array();
msg.clear(); //clear the List Object
msg.add(0, "IMAGE_FILE"); //Add the Type of message with String
msg.add(1, size); //Add the size
msg.add(2, byteArrayOutputStream.toByteArray()); //Add the image file to send
sendMessage(ctx, msg);
ctx.writeAndFlush(msg); //Finally Send it.
}
/*If message in index 0 is Equal to IMAGE_FILE then I need to make it viewable*/
if(msg.get(0).equals("IMAGE_FILE")){
/*
NO IDEA on how to decode it as an Image file
*/
}
}
I keep on searching for any example of this in Netty and I only found the example with Sending via Http
but still I don't know how to do it. By the way, I'm using ObjectEncoder()
and ObjectDecoder(ClassResolvers.cacheDisabled(null))
in my Pipeline.