Send Image with Socket Java

Asked

Viewed 808 times

0

I need to make a program able to send an image to a server (my pc for testing) when pressing a button, I already know how to select the file, but I can’t find/understand anything on the internet to help me send. Does anyone know, or has an example of a simple method that helps me?

  • Need to be with socket? or use a web server to receive the file?

  • Socket, unless q is something very difficult/complicated, so an alternative medium can be used.

  • I will use this documentation to answer: https://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html; of course everything is easier to handle if you use run on Servlets, make HTTP requests for sending data, but I will focus here on pure socket

1 answer

2


Using pure and raw socket, this issue deals with 4 points:

  1. file reading
  2. socket writing
  3. open server socket
  4. reading from a server socket

NOTE: for simplicity and brevity I am not using buffered streams; do not do this in production code without knowing very well that you wish not to use the buffered streams, because they usually help a lot in the performance of reading and writing operations.

File reading

To begin with, we need a path. I’ll call it path, assume that it is a variable that will be correctly filled. To open the file for reading, we use the FileInputStream. To read the first 255 bytes of a file, we can do so:

InputStream in = new FileInputStream(path);

byte[] buffer = new byte[255];
int readBytes = in.read(buffer);
in.close();

Not to forget to close the appeal, we can use the Autocloseable and for a try-with-recources:

try (InputStream in = new FileInputStream(path)) {

    byte[] buffer = new byte[255];
    int readBytes = in.read(buffer);
}

Written in socket

To write in the socket, we need to open a socket and pick up your OutpuStream. To open the socket, take the stream and write "any string", we can do so (documentation:

try (
    Socket bareSocket = new Socket(hostName, portNumber);
    OutputStream out = bareSocket.getOutputStream();
) {
    out.write("uma string qualquer".getBytes());
}

Note that it is always necessary to close everything, so I put everything in a block try-with-resources.

So with the OutputStream open, we need to write everything that is read from the InputStream from the archive to the OutputStream socket. Apache people have already solved this problem for us with the IOUtils.copy. So the writing in the socket with the contents of the file would look like this:

try (
    Socket bareSocket = new Socket(hostName, portNumber);
    OutputStream out = bareSocket.getOutputStream();
    InputStream in = new FileInputStream(path)
) {
    IOUtils.copy(in, out);
}

Opening of server socket

Oracle itself provides an example code using ServerSocket: https://docs.oracle.com/javase/tutorial/networking/sockets/examples/EchoServer.java

There are no secrets here, just create an object ServerSocket using a port and, when it receives a connection call, pick up the socket generated with accept(). Something like that, for a portNumber informed:

try (
        ServerSocket serverSocket = new ServerSocket(portNumber);
        Socket clientSocket = serverSocket.accept();
        InputStream in = clientSocket.getInputStream();
    ) {
    // abri a conexão, agora faça coisas...
}

Reading from a server socket

As we already have the InputStream, we can use it normally as another InputStream any. For example, if we want to write the image to the file in the path pathDestino, I’ll tell you what:

try (
        ServerSocket serverSocket = new ServerSocket(portNumber);
        Socket clientSocket = serverSocket.accept();
        InputStream in = clientSocket.getInputStream();
        OutputStream out = new FileOutputStream(pathDestino, false);
    ) {
    IOUtils.copy(in, out);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.