×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    23

    How to send image from Servlet to Midlet application?

    Hello,
    I do connection between midlet and servlet. my application is:
    1. midlet send query to servlet a
    2. fter that servlet send response to midlet

    until now, the response of servlet is simple text but now I want to send image from servlet to modlet.
    Please tell me how can I do that.
    Thank you in advanced.
    Best regards.

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,772

    Re: How to send image from Servlet to Midlet application?

    Send a PNG, receive it into a byte array, and feed it to Image.createImage((byte[] imageData, int imageOffset, int imageLength)). More advanced approach is experimenting with giving the downstream directly to Image.createImage(InputStream stream)

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    23

    Re: How to send image from Servlet to Midlet application?

    thank you so much for reply,
    Here's what i've done :
    Servlet:
    /*
    * send image to midlet
    */

    BufferedImage img= ImageIO.read(new File("D:/aglets/aglets-2.0.2/image/hotel.png"));;//=getImage("D:/aglets/aglets-2.0.2/image/hotel.png");


    int[] pix = null;
    byte[] pixels = null;

    try
    {

    pix = new int[img.getWidth(null) * img.getHeight(null)];
    PixelGrabber pg = new PixelGrabber(img, 0, 0, img.getWidth(), img.getHeight(), pix, 0, img.getWidth());
    pg.grabPixels();

    pixels = new byte[img.getWidth() * img.getHeight()];
    for(int j=0;j<pix.length;j++)
    {
    pixels[j] = new Integer(pix[j]).byteValue();
    }
    }
    catch(Exception e)
    {
    String error = "PG Exception:" + e;
    log(error);
    }



    out.println(pix.length + "@");// to identify where the arraysize ends.

    for(int j=0; j < pix.length; j++)
    {
    out.println(pixels);
    }
    /* out.flush();
    out.close();

    /*
    *
    */
    }
    Then the midlet:

    /*
    * loading image from servlet
    * */
    InputStream ins = http.openDataInputStream();
    int ch;

    boolean run = true;
    StringBuffer sb = new StringBuffer();
    while(run)
    {
    ch=ins.read();
    if((char)ch == '@')
    {
    run = false;
    }
    else
    {
    sb.append((char) ch);
    }
    }

    String ArraySize = sb.toString();
    int i = Integer.parseInt(ArraySize);
    byte[] newImage = new byte[i];
    for(int n = 0; n < i; n++)
    {
    newImage[n] = (byte)ins.read();
    }
    System.out.println("getImage: Trying to make image...");
    Image Finally = Image.createImage(newImage,1,newImage.length); //Throws invalid arguement exception here.
    System.out.println("getImage: Image Created!");

    /*
    *
    * */
    Unfortunately, the midlet dont draw the image

    Many Thanks In Advance,

  4. #4
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,772

    Re: How to send image from Servlet to Midlet application?

    createImage(byte[]...) expects PNG encoded data, the bytes from the file itself.
    What you have now, pixel data, may work with createRGBImage(...), however pixels[] is too short, it should be width*height*3 for RGB.
    Oh, and no, it is not println how you send binary data... Use out.write(pixels) or something like that.

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    23

    Re: How to send image from Servlet to Midlet application?

    hi,
    i use png image moreover I use pixels = new byte[img.getWidth() * img.getHeight()];
    and in midlet I use
    Image Finally = Image.createImage(newImage,1,newImage.length)
    the result isn't image is code like this
    7854@
    [B@1920c51
    [B@1920c51
    [B@1920c51
    [B@1920c51
    [B@1920c51
    [B@1920c51
    [B@1920c51
    [B@1920c51
    [B@1920c51
    [B@1920c51
    [B@1920c51
    [B@1920c51
    [B@1920c51
    [B@1920c51
    [B@1920c51
    [B@1920c51
    [B@1920c51
    [B@1920c51
    [B@1920c51
    [B@1920c51
    [B@1920c51
    [B@1920c51
    [B@1920c51
    [B@1920c51

  6. #6
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    686

    Re: How to send image from Servlet to Midlet application?

    hsoraya: it seems that like Wizard said you are returning the pixel data of the int[] array you receive from the image getPixels (depanding on your server api and language)
    you dont really need those since that array is bigger than just the image encoded png data.
    In your server code do the following:
    * open the image as a file and not as an image
    * read the binary data (i.e. byte[] array data of the file) till the end of the file.
    * write that byte[] array to the output stream of your midlet connection request.
    In your midlet code do the following:
    * read the byte[] array from the inputstream of the connection using the following code
    Code:
    public static byte[] toByteArray(InputStream input) throws IOException {
             ByteArrayOutputStream output = new ByteArrayOutputStream();
             copy(input, output);
             return output.toByteArray();
         }
    public static final int DEFAULT_BUFFER_SIZE = 1024;
    public static long copy(InputStream input, OutputStream output)  throws IOException {
            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
            long count = 0;
            int n = 0;
            while (-1 != (n = input.read(buffer))) {
                output.write(buffer, 0, n);
                count += n;
            }
            return count;
        }
    * then call Image.createImage(byte[] imageData, int imageOffset, int imageLength); with the byte[] you have read.
    Heard of DVLUP? Join here

  7. #7
    Registered User
    Join Date
    Oct 2013
    Posts
    23

    Re: How to send image from Servlet to Midlet application?

    hello,
    thank you so much shai.i for the reply.
    I use this code to read image in servlet, but unfortunately i have error:
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {

    ServletContext cntx= getServletContext();
    // Get the absolute path of the image
    String filename = cntx.getRealPath("d:/hotel.png");
    String mime = cntx.getMimeType(filename);
    if (mime == null) {
    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    return;
    }

    response.setContentType(mime);
    File file = new File(filename);
    response.setContentLength((int)file.length());

    FileInputStream in = new FileInputStream(file);
    OutputStream out = response.getOutputStream();

    // Copy the contents of the file to the output stream
    byte[] buf = new byte[1024];
    int count = 0;
    while ((count = in.read(buf)) >= 0) {
    out.write(buf, 0, count);
    }
    out.close();
    in.close();

    }

  8. #8
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,772

    Re: How to send image from Servlet to Midlet application?

    That out.close should rather be out.flush, otherwise the code seems to be ok.
    You forgot mentioning the details of the error you get, first of all if it happens on the server side or on the client side.

  9. #9
    Registered User
    Join Date
    Oct 2013
    Posts
    23

    Re: How to send image from Servlet to Midlet application?

    i use this code in servlet but until now i cant display image in midlet
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {


    // reads input file from an absolute path
    String filePath = "D:/aglets/aglets-2.0.2/image/hotel.png";
    File downloadFile = new File(filePath);
    FileInputStream inStream = new FileInputStream(downloadFile);
    // if you want to use a relative path to context root:
    String relativePath = getServletContext().getRealPath("");
    System.out.println("relativePath = " + relativePath);
    // obtains ServletContext
    ServletContext context = getServletContext();
    // gets MIME type of the file
    String mimeType = context.getMimeType(filePath);
    if (mimeType == null) {
    // set to binary type if MIME mapping not found
    mimeType = "application/octet-stream";
    }
    System.out.println("MIME type: " + mimeType);
    // modifies response
    response.setContentType(mimeType);
    response.setContentLength((int) downloadFile.length());

    // forces download
    String headerKey = "Content-Disposition";
    String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
    response.setHeader(headerKey, headerValue);

    // obtains response's output stream
    OutputStream outStream = response.getOutputStream();

    byte[] buffer = new byte[1024];
    int bytesRead = -1;

    while ((bytesRead = inStream.read(buffer)) != -1) {
    outStream.write(buffer, 0, bytesRead);
    }

    inStream.close();
    outStream.close();


    }

  10. #10
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,772

    Re: How to send image from Servlet to Midlet application?

    Have you modified the Midlet-side code to match what you are sending?
    An optimistic test would be
    Code:
    Image Finally=Image.createImage(http.openInputStream());
    however it has no error handling, and the stream should be closed too. Also, Asha platform code may complain if these things happen in the primary/UI thread (it is also true for your current code).
    For a more complete and safe code for receiving binary data, check the official Sun/Oracle documentation, http://docs.oracle.com/javame/config...idp2.0/jsr118/, getViaHttpConnection is the method. For your purpose it could receive data in a secondary thread, then post it to the UI thread an create the image, something like
    Code:
    ...
    final byte[] data = new byte[len];
    while ((bytesread != len) && (actual != -1)) {
       actual = is.read(data, bytesread, len - bytesread);
       bytesread += actual;
    }
    Display.getDisplay().callSerially(
       new Runnable() {
          public void run() {
             Image Finally=Image.createImage(data,0,data.length);
             ... store it somewhere ...
          }
       });
    ...
    For non-Asha you can use the thing without this callSerially, Runnable magic. For Asha, they are really needed, see http://developer.nokia.com/Resources...-commands.html and http://developer.nokia.com/Resources...tive-apis.html for the explanation.
    Last edited by wizard_hu_; 2013-12-06 at 16:04.

  11. #11
    Registered User
    Join Date
    Oct 2013
    Posts
    23

    Re: How to send image from Servlet to Midlet application?

    hi,
    yes I modify the aglet side like this
    public AppendImage()
    {
    try
    {
    http = (HttpConnection) Connector.open(url);

    //----------------
    // Client Request
    //----------------
    // 1) Send request method
    http.setRequestMethod(HttpConnection.GET);

    // 2) Send header information - none

    // If you experience connection/IO problems, try
    // removing the comment from the following line
    //http.setRequestProperty("Connection", "close");

    // 3) Send body/data - data is at the end of URL

    //----------------
    // Server Response
    //----------------

    InputStream ins = http.openDataInputStream();
    int ch;

    boolean run = true;
    StringBuffer sb = new StringBuffer();
    while(run)
    {
    ch=ins.read();
    if((char)ch == '@')
    {
    run = false;
    }
    else
    {
    sb.append((char) ch);
    }
    }

    String ArraySize = sb.toString();
    int i = Integer.parseInt(ArraySize);
    byte[] newImage = new byte[i];
    for(int n = 0; n < i; n++)
    {
    newImage[n] = (byte)ins.read();
    }
    System.out.println("getImage: Trying to make image...");
    try
    {
    Image Finallyimage = Image.createImage(newImage,0,newImage.length); //Throws invalid arguement exception here.
    System.out.println("getImage: Image Created!");
    }
    catch (Exception e)
    {
    System.err.println("Can`t load the image : ");
    }

    }
    catch (Exception e)
    {
    System.err.println("Can`t load the image : ");
    }


    /* try
    {
    image = Image.createImage("/hotel.png");
    } catch (Exception e){ }
    imageItem = new ImageItem("This is the IMAGE_ITEM Application",
    image, ImageItem.LAYOUT_DEFAULT, "image");
    */
    }

  12. #12
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,772

    Re: How to send image from Servlet to Midlet application?

    Even if you had any kind of @-delimited textual data in the past, the servlet code variants in #7 and #9 do not have it any more.

  13. #13
    Registered User
    Join Date
    Oct 2013
    Posts
    23

    Re: How to send image from Servlet to Midlet application?

    hi,
    I think the problem is in servlet because when I put this link in the browser "http://localhost:8080/servlets/sendimage/hotel.png" I have error but when i excute the servlet from eclipse I have no error and i have new window to downloawd the image

  14. #14
    Registered User
    Join Date
    Oct 2013
    Posts
    23

    Re: How to send image from Servlet to Midlet application?

    Hi,
    now i resolve my problem, but unfortunately I have other problem. this last is how can I send data and image from servlet to midlet.
    thank you in advanced

  15. #15
    Super Contributor
    Join Date
    Mar 2003
    Location
    Finland
    Posts
    9,570

    Re: How to send image from Servlet to Midlet application?

    Quote Originally Posted by hsoraya View Post
    Hi,
    now i resolve my problem, but unfortunately I have other problem. this last is how can I send data and image from servlet to midlet.
    thank you in advanced
    Do you want to send them both in the same response? If so, you need to package/encode them somehow. E.g., zip a data/text file with the image and have the MIDlet receive the zip file and unpack it. You could also simply return the image, and the data in the same response encoded in custom HTTP-headers, and the MIDlet should check the headers for the data as it receives the image.

Similar Threads

  1. send object from servlet to MIDlet
    By uraravind in forum Mobile Java General
    Replies: 1
    Last Post: 2011-01-07, 16:20
  2. [Need Help] How to Send Vector from Servlet to Midlet
    By iamjavanese in forum Mobile Java Networking & Messaging & Security
    Replies: 0
    Last Post: 2010-01-06, 08:30
  3. Sending a image from midlet to a servlet. Help on recoding image.
    By Kaisser in forum Mobile Java Networking & Messaging & Security
    Replies: 0
    Last Post: 2008-10-06, 15:45
  4. how to send SMS from Servlet to MIDlet
    By b75mihai in forum Mobile Java Networking & Messaging & Security
    Replies: 2
    Last Post: 2005-10-04, 13:18
  5. hot to send and SMS from a servlet to a MIDlet
    By b75mihai in forum General Messaging
    Replies: 0
    Last Post: 2005-10-04, 11:17

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×