를 사용하여 이미지를 수신 :보내기 내가 로컬 네트워크에 아래의 코드를 사용하여 이미지 파일을 전송할 수 있습니다 <code>ServerSocket</code> 및 <code>Socket</code> 자바에서 소켓
는
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();
}
}
Netty
에서 Soc를 사용하면 어떻게됩니까? 케?
내 처리기
@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))
을 사용하고 있습니다.
바로 지금 당장 직면하고 있습니다. 감사합니다! – Polar
'ChuckedWriteHandler'를 사용하는 데 문제가 있습니다. 이미지와 텍스트가 포함 된 ArrayList로 보내야합니다. – Polar
안녕하세요! 'ChunckedWriteHandler'를 사용하는 데 어려움을 겪어 왔습니다. 내 질문에 대한 답변을 찾고 싶습니다. https://stackoverflow.com/questions/47792126/sending-large-object-containing-string-and-image-file 감사! – Polar