Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java send an image over a socket

public class Send {

    public static void main(String[] args) throws Exception {
        Socket socket = new Socket("localhost", 13085);
        OutputStream outputStream = socket.getOutputStream();

        BufferedImage image = ImageIO.read(new File("C:UsersJakubPictures	est.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();
        System.out.println("Flushed: " + System.currentTimeMillis());

        Thread.sleep(120000);
        System.out.println("Closing: " + System.currentTimeMillis());
        socket.close();
    }
}


public class Receive {

    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(13085);
        Socket socket = serverSocket.accept();
        InputStream inputStream = socket.getInputStream();

        System.out.println("Reading: " + System.currentTimeMillis());

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

        System.out.println("Received " + image.getHeight() + "x" + image.getWidth() + ": " + System.currentTimeMillis());
        ImageIO.write(image, "jpg", new File("C:UsersJakubPictures	est2.jpg"));

        serverSocket.close();
    }

}
Comment

PREVIOUS NEXT
Code Example
Java :: java how to generate guid 
Java :: java for schleife 
Java :: finally block does not complete normallyJava(536871096) 
Java :: polar to cartesian java 
Java :: how to create a java jframe 
Java :: leap year program in java 
Java :: javafx live date and time 
Java :: null checker on addAll java 
Java :: how to set lowered bezels in jlabel 
Java :: java mouselistener get coordinates 
Java :: k combinations for range 1 through n 
Java :: convert string to char array in java 
Java :: java cartesian to polar 
Java :: Bukkit debug message 
Java :: foreach not applicable to type - binary tree sort 
Java :: method to check parameters in java 
Java :: how to show full search bar in android studio 
Java :: address book java program to add edit and delete 
Java :: list java processes 
Java :: java loop through arraylist 
Java :: java print type of object 
Java :: Date from String java3 
Java :: java initialize array with same value 
Java :: java real random 
Java :: How to generate all subsets of a given set in Java? 
Java :: how to find power of a number in java 
Java :: java switch 
Java :: hashtable contains key java 
Java :: java toast 
Java :: object to array java 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =