gpt4 book ai didi

java - 使用Socket发送和接收图像

转载 作者:太空宇宙 更新时间:2023-11-04 10:51:44 26 4
gpt4 key购买 nike

在带有 ServerSocketSocket 的 Java 中,我可以在本地网络中使用以下代码传输图像文件:

发送

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();
}
}

接收

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();
}

}

如何使用 Socket 在 Netty 中做到这一点?

我的处理程序

@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
*/

}


}

我继续在 Netty 中搜索任何这样的示例,我只找到了通过 Http 发送的示例,但我仍然不知道该怎么做。顺便说一句,我在管道中使用 ObjectEncoder()ObjectDecoder(ClassResolvers.cacheDisabled(null))

最佳答案

您的方法很好,但通常在传输大文件(不仅限于图像文件)时可能会遇到问题。我建议您查看 here 中的 ChuckedWriteHandler ,该文档写得很好,可以遵循。

关于java - 使用Socket发送和接收图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47765767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com