Search code examples
javadata-transferhessian

Image ransfer using hessian protocol from client's folder to tomcat server


My goal is to upload a image(.jpg or .png) from client's folder to tomcat6 server through hessian protocol. And do image processing using opencv on server, then return the image back to client.

Question1. Is the following transfering steps correct?

  • put a test.jpg image on client's folder
    --> convert the test.jpg in client.java (main.java) class to BufferedImage
    --> convert the BufferedImage to mat or Iplimage in server for using openCV.

I have set a hello world sample from Simple Messaging Example using Hessian , and searched from Hessian with large binary data and other websites, but still dont know how to use it!

Question2. Is there a related Java sample code?

(I am using ubuntu12+netbeans7.2)


Solution

  • It sounds like you probably want to treat the image as a byte stream on the client, instead of using BufferedImage. After processing, you can do whatever you want, but it'll be easier to use hessian if you just send the file contents.

    Hessian understand InputStream as a type. So your minimal method call API can look like

    InputStream convert(InputStream upload);
    

    The client would open an input stream to the original file and send that input stream directly:

    InputStream is = new FileInputStream("test.jpg");
    InputStream resultIs = hessianProxy.convert(is);
    .... // save the result
    

    A bit of caution that the hessian response connection will still be live until you finish reading the input stream, so you need to read it immediately. (It's not buffered, which is one reason it can be efficient.)

    On the server, you need to read from the input stream (again, immediately). And as a result, return an InputStream that reads from your converted image:

    InputStream convert(InputStream is) {
      ... // read from 'is' to your internal data
      InputStream result = ... // process
      return result;
    }
    

    You'll want to make sure the result input stream closes anything automatically on the end of file. You might want to create a wrapper to call close().