Urgent help:send image over network using rmi

hi all,
i have few question about send image using rmi.
1) should i use ByteArrayOutputStream to convert image into byte array before i send over network or just use fileinputstream to convert image into byte array like what i have done as below?
public class RemoteServerImpl  extends UnicastRemoteObject implements RemoteServer
  public RemoteServerImpl() throws RemoteException
  public byte[] getimage() throws RemoteException
    try{
       // capture the whole screen
       BufferedImage screencapture = new Robot().createScreenCapture(new     Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );
       // Save as JPEG
       File file = new File("screencapture.jpg");
       ImageIO.write(screencapture, "jpg", file);
        byte[] fileByteContent = null;
       fileByteContent = getImageStream("screencapture.jpg");
       return fileByteContent;
    catch(IOException ex)
  public byte[] getImageStream(String fname) // local method
    String fileName = fname;
    FileInputStream fileInputStream = null;
    byte[] fileByteContent = null;
      try
        int count = 0;
        fileInputStream = new FileInputStream(fileName);  // Obtains input bytes from a file.
        fileByteContent = new byte[fileInputStream.available()]; // Assign size to byte array.
        while (fileInputStream.available()>0)   // Correcting file content bytes, and put them into the byte array.
           fileByteContent[count]=(byte)fileInputStream.read();
           count++;
       catch (IOException fnfe)
     return fileByteContent;           
}2)if what i done is wrong,can somebody give me guide?else if correct ,then how can i rebuild the image from the byte array and put it in a JLable?i now must use FileOuputStream but how?can anyone answer me or simple code?
thanks in advance..

Hi! well a had the same problem sending an image trough RMI.. To solve this i just read the image file into a byte Array and send the array to the client, and then the client creates an imegeIcon from the byte Array containing the image.. Below is the example function ton read the file to a byte Array (on the server) and the function to convert it to a an imageIcon (on the client).
//      Returns the contents of the file in a byte array.
    public static byte[] getBytesFromFile(File file) throws IOException {
        InputStream is = new FileInputStream(file);
        // Get the size of the file
        long length = file.length();
        // You cannot create an array using a long type.
        // It needs to be an int type.
        // Before converting to an int type, check
        // to ensure that file is not larger than Integer.MAX_VALUE.
        if (length > Integer.MAX_VALUE) {
            // File is too large
        // Create the byte array to hold the data
        byte[] bytes = new byte[(int)length];
        // Read in the bytes
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length
               && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
            offset += numRead;
        // Ensure all the bytes have been read in
        if (offset < bytes.length) {
            throw new IOException("Could not completely read file "+file.getName());
        // Close the input stream and return bytes
        is.close();
        return bytes;
    }to use this function simply use something like this
public byte[] getImage(){
byte[] imageData;
          File file = new File("pic.jpg");
          // Change pic.jpg for the name of your file (duh)
          try{
               imageData = getBytesFromFile(file);
               // Send to client via RMI
                        return imageData;
          }catch(IOException ioe){
                       // Handle exception
                       return null; // or whatever you want..
}and then on the client you could call a function like this
public ImageIcon getImageFromServer(){
     try{
          // get the image from the RMI server
          byte[] imgBytes = myServerObject.getImage();
          // Create an imageIcon from the Array of bytes
          ImageIcon ii = new ImageIcon(imgBytes);
          return ii;
     }catch(Exception e){
          // Handle some error..
          // If yo get here probably something went wrong with the server
          // like File Not Found or something like that..
          e.printStackTrace();
          return null;
}Hope it helps you..

Similar Messages

  • Sending images over network socket

    I'm working on a java web server. So far i have managed to get it to send html documents sucessfully to the client web browser, using a BufferedReader, and a PrintWriter object.
    But how should i send images over the socket? using this method doesn't work. Any suggestions?
    Thanks in advance.
    hornetau

    I did it first. You may pay me $10 and get XM2 WebServer 1.2 from my company.
    Ok, I'll help ya out here...
    HTTP protocol in Internet Explorer is "juiced up" meaning that it does not require HTTP data to be correctly sent. To send an image to be deisplayed...
    <html>
    <img src="theImage.gif"></img>
    </html>
    Now, the server will see a GET request like this...
    GET /theImage.gif HTTP/1.1
    Accepts: Image/jpeg, Image/gif, ...
    Your web server (in the IE case just needs to send)...
    output.println(data);
    The data object is a string, that represents the contents of the image.
    To do that, just get the correct File object, connect reader to it, then loop it
    until the reader is no longer ready (reader.ready() != true), as it loops, just append
    the readLine() command to the end of the data string and it will be ok.
    Now this works on IE just fine, using vary small file sizes due to it being loaded directly into
    memory. Casing problem if your app has only 500K of memory and the file size is 700K, 500-700=-200K, so use only small file sizes with this method.
    There is also the URLConnection and HttpURLConnection classes to use that will do this better, but
    they dont have any real way of getting the file's data - that's still YOUR job.

  • Trouble sending images over sms

    Anyone having trouble sending images over txt messages?

    I did it first. You may pay me $10 and get XM2 WebServer 1.2 from my company.
    Ok, I'll help ya out here...
    HTTP protocol in Internet Explorer is "juiced up" meaning that it does not require HTTP data to be correctly sent. To send an image to be deisplayed...
    <html>
    <img src="theImage.gif"></img>
    </html>
    Now, the server will see a GET request like this...
    GET /theImage.gif HTTP/1.1
    Accepts: Image/jpeg, Image/gif, ...
    Your web server (in the IE case just needs to send)...
    output.println(data);
    The data object is a string, that represents the contents of the image.
    To do that, just get the correct File object, connect reader to it, then loop it
    until the reader is no longer ready (reader.ready() != true), as it loops, just append
    the readLine() command to the end of the data string and it will be ok.
    Now this works on IE just fine, using vary small file sizes due to it being loaded directly into
    memory. Casing problem if your app has only 500K of memory and the file size is 700K, 500-700=-200K, so use only small file sizes with this method.
    There is also the URLConnection and HttpURLConnection classes to use that will do this better, but
    they dont have any real way of getting the file's data - that's still YOUR job.

  • How to add an image over another using af:image

    How to add an image over another using af:image
    Thanks,
    Veera

    i have a image which is black strip. i have added that to af:image
    <af:image source="image1" id="image" />
    on the black strip, i need to add company logo.
    how to achieve it.
    Thanks,
    Veera.

  • How to send images through PI using SOAP adapter

    hi,
    Can anybody tell me how to send images through PI using SOAP adapter.
    Regards ,
    Loveena

    Hi Loveena,
    only as attachments of a SOAP message.
    Regards,
    Udo

  • Sending Connection Object over the Network using RMI

    Hi,
    How can a Connection object be sent over the network and run on another JVM. I need to hold connection object to execute processes one after other, that require Oracle connection without ever connecting again. I do not have J2EE container or webserver setup to hold connection/connectionpool,but need to run the process on command line. I am using RMI infrastructure to pass parameters/return values but connection object is not serializable and connot be marshalled and failing. Please explain, if there is another way using JDK 1.4
    Sudheer

    I think that what you want to do is connect to the database on the RMI server object, then use the server object from your remote clients to execute the processes you require to rrun.

  • How can i taransfer an image over network through Sockets

    If any one have the exsperience for tranfering the image over the network by using socket plz help me immediatly

    You have to write a Server application and a Client application.
    And connect the Client to the Server.
    Then you can send whatever you want in either direction.

  • Urgent help needed for XML Tags using XMLForest()

    Folks
    I need some urgent help regarding getting use defined tag in your
    XML output.
    For this I am using XMLElement and XMLForest which seems to work fine
    when used at the SQL prompt but when used in a procedure throws and error
    SQL> Select SYS_XMLAGG(XMLElement("SDI",
                                       XMLForest(sdi_num)))
         From sdi
         where sdi_num = 22261;- WORKS FINE
    But when used in a procedure,doesnt seem to work
    Declare
        queryCtx  DBMS_XMLQuery.ctxType;
        v_xml     VARCHAR2(32767);
        v_xmlClob CLOB;
        BEGIN
        v_xml:='Select SYS_XMLAGG(XMLElement("SDI",
                                             XMLFOREST(sdi_num)))
        From sdi
        where sdi_num = 22261';
        queryCtx :=DBMS_XMLQuery.newContext(v_xml);
        v_xmlClob :=DBMS_XMLQuery.getXML(queryCtx);
        display_xml(v_xmlClob);
    End;
    CREATE OR REPLACE PROCEDURE  display_xml(result IN OUT NOCOPY CLOB)
    AS
         xmlstr varchar2(32767);
         line varchar2(2000);
    BEGIN
         xmlstr:=dbms_lob.SUBSTR(result,32767);
         LOOP
         EXIT WHEN xmlstr is null;
         line :=substr(xmlstr,1,instr(xmlstr,chr(10))-1);
         dbms_output.put_line('.'||line);
         xmlstr := substr(xmlstr,instr(xmlstr,chr(10))+1);
         END LOOP;
    end;
    SQL> /
    .<?xml version = '1.0'?>
    .<ERROR>oracle.xml.sql.OracleXMLSQLException: Character ')' is not allowed in an
    XML tag name.</ERROR>
    PL/SQL procedure successfully completed.
    SQL>HELP is appreciated as to where I am going wrong?

    Hi,
    if you want to transform something to something else, you should declare, what is your source.
    I would prefer to use plain XSL-Transformations, because you have a lot more options to transform your source and you can even better determine, how your output should looks like.
    Kind regards,
    Hendrik

  • How to send images through java or RMI.?

    Hello,
    I have a problem with RMI. I had created a canvas on which you can draw anything then when clicking send button this drawing should be reach to another client(s). so please guide me how to do it. and also please give some code if possible.
    thanks very much.
    umesh m joshi

    Hello RoopaSri:
    Umm, I don't think it will help in his case. He means to send a drawing from a canvas and not a image file.So that is back to his question
    hello umjosh ,
    you can get an image object from the Graphics class and RMI the bytes gotten from the image object. U can find in this forum on how to save the canvas. instead of saving, u can send the bytes.
    eg Imageicon imgIcon=new Imageicon("imagefile.jpg");
    and send it to the remote object. Use
    ImageIcon.getImage can used to exrtract the image.
    Hope this helped.
    Regards,
    Roopa Vittal
    developer technical Support
    SUN Microsystems.

  • How to transfer images over network?

    Hello,
    I've built a program in LV 7.1 which taking a snap shot from a USB webcam and save it as JPEG file.
    As I already said I am using LV 7.1 with NI VISION 7.1.
    My program has to take the picture from the webcam and send it via serial visa connection. (TCP \ IP is also an option)
    Regards,
    Rotem

    There are lots of ways to transfer files over the network. The easiest, in my opinion, is to use TCP/IP and set up a network share or mapped drive between the two machines and then transfer the file by using copy. Of course, there are lots of situations where setting up a network share isn't apropriate.
    If network share isn't an option, and you want to use serial, you should probably use a protocol such as Xmodem, Ymodem or Zmodem to transfer the file. LabVIEW doesn't have direct support for these protocols, although I know someone sells (or at least used to sell, I couldn't find it with a quick search) a modem toolkit for LabVIEW with these protocols implemented. There are also ActiveX servers that implement these protocols that you could download or purchase and then access from LabVIEW.
    If you want to go the TCP/IP route, an FTP server would be an easy option. You'd need to run an FTP server on the target machine, but then you could just use the LabVIEW FTP VIs or call an FTP terminal through command line or ActiveX.
    Of course, you could implement your own file transfer protocol using VISA, datasocket or the TCP/IP VIs, but this last option is quite a bit of work to solve a problem when there are plenty of programs out there to solve it for you. Re-inventing the wheel if you will. While it's not all that difficult to read in a file, transfer it using one of the communication APIs, and then write it back to a file on your client machine, you'll either have to implement, or go without, a lot of the features and safeguards, like error checking, which are built into other file transfer protocols. Also, remember that you'll have to have a LabVIEW application running on each end, so you'll have to implement both halves of the solution (as opposed to options like FTP or the network share, where you only have to run a VI on one computer).
    Hope that helps,
    Ryan K.

  • Fill error -50 Can't import over network using new account

    login as admin and can import music from my PC over wireless net (I just tried it). I set up a new account which is also administrator. As far as I can tell the two accounts are identical. I am leaving the files in their original location and not copying them. Tried importing but i get unknown file error -50. When attempting this on new account, prompts are slightly different, tho. It doesn't prompt me to select from available shares like in original admin account, but i can select network and then available shares manually. I'm using mp3 encoding. Thanks for any help

    Kelly--
    Thanks Charlie. tested theory right away. I made
    new shared folder right on the root c:/ directory.
    Put a song in there with a short name and tried to
    import it. Got same error -50. So maybe that's not
    it. Any other ideas?
    What happens if you don't use iTunes, but just try to copy a file from the Windows share using the Finder? If you still can't copy from the one account, then it's a matter of how the Windows file sharing is set up in that account.
    That error is a pretty general one, and what you're doing involves so many parts of the OS, from networking to authentication, to QuickTime, that anything you can do to narrow it down will help.
    Can you describe in more detail the process you are going through to access the songs on your PC from both accounts? The more detail, the better...
    Remember,... i can import these same files from same
    folders (so, long filename and path isn't and issue
    here) from another account on this mac.
    That points to a setting that's different between accounts. Unfortunately, because of that nature of the error, until you can really narrow it down, it's going to be kind of tough to figure out why this error occurs.
    charlie

  • Problem with sending message over network

    ssc = (ServerSocketConnection) Connector.open("socket://:5000");
    sc = (SocketConnection) Connector.open("socket://localhost:5000");my app works over the network in the emulator, but wont work on real devices, or do i just need to pair somehow with the device before i start.
    are the above lines okay is it meant to be localhost that's what they use in the socket part of the network demo that comes with the wireless toolkit, anyone have any ideas why that app doesnt seem to work for me on the real devices.
    i know for bluetooth you have to pair with the device before running the application do you have to do something like this

    Vishal,
    Couple of questions. 
    How are you sending this message.  Is it a message that you create and send Internally with an attachment.
    As you reply me, please also check if the <b>Attachments folder</b> has been set under <b>Administration > System Initialization > General Settings.....Path Tab</b>
    SBO tries to copy the attachment from the location you select to the Path defined for Attachments.
    Let me know
    Suda

  • Need Urgent help--Display image as background of an email

    Dear All,
    Iam new to Java mail API.
    Someone please let me know if it is possible to make an image display as a background of an email using java mail.
    I had tried using below code.
    //code
    message.setContent( "<html><body><table width=\"100%\" height=\"90%\" border=\"0\" cellspacing=\"0\" cellpadding=\"20\" background=\"https://ttslemc.tatatel.co.in/EMC_FEEDBACK/imagepool/indicom_logo.jpg\"><tr><td>
    "+ content+ "
    </td></tr></table></body></html>",
    "text/html");
    //here content is a variable containing mail content captured from a html text area
    //code
    This is displaying me the image but the content formatting is lost.Everything appears in single line.
    Is it possible to have a background image and the content formatted properly for a html mail using java mail api.
    Please help me.....ASAP

    First, realize that how a message is displayed is up to the message reader being used.
    You can't control it, you can only provide suggestions.
    Using html text with a reference to an external image is a good start. I don't understand
    what you mean when you say "the content formatting is lost". Is none of the html formatting
    having any effect? Are you seeing the raw html instead of the formatted text?

  • Beginner imageIO question (pulling up image over network vs local)

    Greets,
    I am new to java but not too new to programming. I've googled the heck out of this question and checked the java 2D api and haven't had much luck yet. Take a look and let me know what you think:
    I would like to pull up an image file and display it in a jLabel, it works fine if i am pulling up a local file as such:
    BufferedImage img = null;
            try {
                img = ImageIO.read(new File("/home/steve/pic.gif"));
            } catch (IOException e) {
                e.printStackTrace();
            }But what i really want it to do, is pull up a file over the local network, like so:
    BufferedImage img = null;
            try {
                img = ImageIO.read(new File("smb://192.168.1.64/images/pic.gif"));
            } catch (IOException e) {
                e.printStackTrace();
            }However, when i try this second one, i get the following error:
    javax.imageio.IIOException: Can't read input file!
            at javax.imageio.ImageIO.read(ImageIO.java:1275)
            at mypkg.MainForm.load(MainForm.java:143)
            at mypkg.MainForm.<init>(MainForm.java:27)
            at mypkg.MainForm$1.run(MainForm.java:111)
            at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)Can anyone point me in the right direction or give me a code snippet? Much appreciated.

    Paul,
    All definately valid points. I was able to get it to work if i mounted the smb share to my local filesystem but essentially i am trying to make the application portable so i dont have to mount a drive on the target machine (or create a windows drive map, etc). Although read(URL input) isnt exactly what i was looking for, it works good enough and gets the same end result. i just have to copy the image folder and serv it up via apache instead of samba.
    Thanx for your input.

  • Sending HashMap over network

    hi there,
    Is there any way I can get around this HashMap bug?
    http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4756277
    I tried to write a HashMap object over a network and the following exception was generated:
    java.io.NotSerializableException: java.util.HashMap$EntrySet
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
    Regards,

    Due to this bug you can't use serialization mechanism on HashMap. A workaround should be to implement a SerializedHashMap class which extends HashMap and reimplement method for serialization. Methods to reimplement are:private void writeObject (java.io.ObjectOutputStream out) throws IOException;and
    private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;You can found useful tips of about write your code here: java.sun.com/docs/books/tutorial/essential/io/objectstreams.html

Maybe you are looking for

  • Automatically adding free goods in SO

    I have a finished good that I would like to sell with a third-party free good. I have set up both materials in MM. How do I link the 2 together such that when I create a SO for the finished good, the free good will be added automatically? Is there an

  • Spliting a table in to two table based on the groups in SSRS

    Hi, I am trying to a split a rows accross two tables based on a group. So far I have added a list and added a row group and grouped it using lineof business Inside my list i have two identical tables so i can display the data for the first table i ha

  • How to access module parameters?

    Hi, I'm developing own module for XI File Adapter. I found in the weblogs how to get source file name. Thanks. But how do I get custom parameter specified for the module? Where could I check all readable parameters, apart from source file name? For i

  • Why does the export quality of my vector image degrade when effects are applied?

    Hi guys, I am unsure as to where I am going wrong... Basically, I created a patterned background on AI and imported it straight into AE, where I set keyframes for it to rotate. I added fractal noise (got my desired effect). On separate layers in the

  • WRT310N help needed

    Hi I recently bought a WRT310N router, and I have some problems with my connection. I have 2 pcs, one downstairs (where my modem is connected to router via ethernet cable and has LELA 3.0 build: 8122.29) and one upstairs (connected by WUSB54GC), both