0

i am creating a desktop client server application, in which i am capturing the frames, in jpg images, rendered by the renderer and storing them on the client side. now i need to upload the images to the server. i tried this by placing a separate thread for every captured image to upload it directly to the server but it was very time consuming. Also i tried to upload all the images from the client after the capturing is stopped, but that is not the case i want.

SO is there a way to upload the directly captured images to server effectievely.

for capturing images i am using BufferedImage and ImageIO.write methods

Thanks in advance

Foramkumar Parekh
  • 421
  • 1
  • 6
  • 26
  • how are you uploading each frame[image] from client to server,I mean using which protocol ? – Dev Nov 09 '15 at 10:01
  • 1
    How are you sending the image to server? can you post the code? – Ravindra babu Nov 09 '15 at 10:16
  • I am interested in sending part. Is it socket or servlet or some thing else? If you are sending through, you can fine tune buffer size (8192 or some big number) and send it as per this post : http://stackoverflow.com/questions/32251895/java-file-transfer-file-to-server/32252321#32252321 – Ravindra babu Nov 09 '15 at 10:21
  • ok thanks @ravindra i will try with socket – Foramkumar Parekh Nov 09 '15 at 10:24
  • @dev i am using http protocol to upload the images – Foramkumar Parekh Nov 09 '15 at 10:25
  • try to use socket , create a socket client and socketServer , its the fastest way to upload image becase communication over socket is asynchronous while communication over is synchronous . – Dev Nov 09 '15 at 10:29
  • visit this links http://stackoverflow.com/questions/12242033/sending-image-over-socket http://stackoverflow.com/questions/8680409/read-image-from-socket – Dev Nov 09 '15 at 10:30
  • how about encoding your image into base64 and sending the stream to server, its effective I think – karan Nov 09 '15 at 11:41
  • @KaranMer yes its effective but not as fast as socket, because encoding and decoding image to and from Base64 will consume time on client and server side. – Dev Nov 09 '15 at 11:54
  • what about transmission time, it will be much less if transferred using socket. – karan Nov 09 '15 at 11:57
  • @Karan Mer as per my experience Base64 string size of a .jpg image is always grater then actual size visit this link for more detail http://stackoverflow.com/questions/11402329/base64-encoded-image-size – Dev Nov 09 '15 at 17:16
  • yes as far as base64 is being transfered to server over http it will be slower to same transfer over socket – Dev Nov 09 '15 at 17:18

1 Answers1

0

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.

Dev
  • 2,326
  • 24
  • 45
  • Thanks dev, this helped me to upload single image, but as i mentioned, i have multiple images to be uploaded, i am facing the connection reset problem for that – Foramkumar Parekh Nov 09 '15 at 13:57
  • yes, i am uploading it in single thread. Actually i wanna send multiple image files in a single connection, cos creating connection for every image is not feasible. – Foramkumar Parekh Nov 09 '15 at 14:49