How to Send an Image from a Java Socket Client to a Server
This guide demonstrates a complete Java socket implementation that lets a client read an image file, stream its bytes over a TCP connection to a server, and have the server reconstruct and save the picture to disk.
The article builds on a previous example of bidirectional string communication using Java sockets and adds a concrete method for transferring binary image data.
Server side
A static method getImageFromClient() creates a ServerSocket listening on port 8088, accepts a client connection, and reads the incoming byte stream into a 2 KB buffer. The data is written to D:\badao.png until readLength equals -1, after which all streams and sockets are closed.
public static void getImageFromClient() throws IOException {
byte[] byteArray = new byte[2048];
ServerSocket serverSocket = new ServerSocket(8088);
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
int readLength = inputStream.read(byteArray);
FileOutputStream imageOutputStream = new FileOutputStream(new File("D:\\badao.png"));
while (readLength != -1) {
imageOutputStream.write(byteArray, 0, readLength);
readLength = inputStream.read(byteArray);
}
imageOutputStream.close();
inputStream.close();
socket.close();
serverSocket.close();
}Client side
The client defines sendImageToServer(), which opens the local image file C:\Users\admin6\Desktop\gzh.png via a FileInputStream, connects to the server at localhost:8088, and streams the file contents using the same 2 KB buffer. After the loop finishes, the output stream, file stream, and socket are closed.
public static void sendImageToServer() throws IOException {
String imageFile = "C:\\Users\\admin6\\Desktop\\gzh.png";
FileInputStream imageStream = new FileInputStream(new File(imageFile));
byte[] byteArray = new byte[2048];
System.out.println("socket begin =" + System.currentTimeMillis());
Socket socket = new Socket("localhost", 8088);
System.out.println("socket end =" + System.currentTimeMillis());
OutputStream outputStream = socket.getOutputStream();
int readLength = imageStream.read(byteArray);
while (readLength != -1) {
outputStream.write(byteArray, 0, readLength);
readLength = imageStream.read(byteArray);
}
outputStream.close();
imageStream.close();
socket.close();
}Execution order
First run the server’s main method to start listening, then run the client’s main method. After the transfer completes, the image appears at the specified server path (e.g., D:\badao.png), confirming successful binary transmission.
An illustrative GIF shows the process and the resulting file on the server.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
The Dominant Programmer
Resources and tutorials for programmers' advanced learning journey. Advanced tracks in Java, Python, and C#. Blog: https://blog.csdn.net/badao_liumang_qizhi
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
