Bienvenido a nuestro sitio, en este sitio vas a hallar la solucíon que andabas buscando.
Ejemplo: java envía una imagen a través de un socket
publicclassSendpublicstaticvoidmain(String[] args)throwsExceptionSocket socket =newSocket("localhost",13085);OutputStream outputStream = socket.getOutputStream();BufferedImage image =ImageIO.read(newFile("C:\Users\Jakub\Pictures\test.jpg"));ByteArrayOutputStream byteArrayOutputStream =newByteArrayOutputStream();ImageIO.write(image,"jpg", byteArrayOutputStream);byte[] size =ByteBuffer.allocate(4).putInt(byteArrayOutputStream.size()).array();
outputStream.write(size);
outputStream.write(byteArrayOutputStream.toByteArray());
outputStream.flush();System.out.println("Flushed: "+System.currentTimeMillis());Thread.sleep(120000);System.out.println("Closing: "+System.currentTimeMillis());
socket.close();publicclassReceivepublicstaticvoidmain(String[] args)throwsExceptionServerSocket serverSocket =newServerSocket(13085);Socket socket = serverSocket.accept();InputStream inputStream = socket.getInputStream();System.out.println("Reading: "+System.currentTimeMillis());byte[] sizeAr =newbyte[4];
inputStream.read(sizeAr);int size =ByteBuffer.wrap(sizeAr).asIntBuffer().get();byte[] imageAr =newbyte[size];
inputStream.read(imageAr);BufferedImage image =ImageIO.read(newByteArrayInputStream(imageAr));System.out.println("Received "+ image.getHeight()+"x"+ image.getWidth()+": "+System.currentTimeMillis());ImageIO.write(image,"jpg",newFile("C:\Users\Jakub\Pictures\test2.jpg"));
serverSocket.close();
Sección de Reseñas y Valoraciones
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)