Image uploading Over socket is the fastest way to upload image on server because data will be passed to server as byte stream.
Below is simple Socket Client and Socket Sever to achieve Image upload
Client
public class ImageUploadSocketClient {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("localhost",6666);
OutputStream outputStream = socket.getOutputStream();
BufferedImage image = ImageIO.read(new File("path to image /your_image.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();
}
}
Server
public class ImageUploadSocketRunnable implements Runnable{
public static final String dir="path to store image";
Socket soc=null;
ImageUploadSocketRunnable(Socket soc){
this.soc=soc;
}
@Override
public void run() {
InputStream inputStream = null;
try {
inputStream = this.soc.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(dir+System.currentTimeMillis()+".jpg"));
inputStream.close();
} catch (IOException ex) {
Logger.getLogger(ImageUploadSocketRunnable.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(13085);
while(true){
Socket socket = serverSocket.accept();
ImageUploadSocketRunnable imgUploadServer=new ImageUploadSocketRunnable(socket);
Thread thread=new Thread(imgUploadServer);
thread.start();
}
}
}
On server You should create different thread for different client socket in this way you can achieve concurrent image upload from different clients.
Hope above example will help you.