Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    6

    Converting an image into bytes and writing the bytes into a file in glassfish folders

    Hello everybody,

    I am trying to convert an image into byte for sending it to Glassfish....the project compiles successfully but it doesnt writes into a file in Glassfish folders.
    But when i tried writing a simple text instead of image bytes....the code successfully wrote into Glassfish but now i tried converting image into bytes and then sending those bytes...it is not writing it a file...

    Please give me suggestions.

    CODE:

    public void send() {
    new Thread(new Runnable() {

    public void run() {

    //Here starts the loading part

    byte[] imgBytes = null;
    ByteArrayOutputStream baos = null;
    InputStream in = null;
    try {
    baos = new ByteArrayOutputStream();
    in = this.getClass().getResourceAsStream("/duke.png"); //this image is inside my mobile application
    byte[] buffer = new byte[1024];
    int n = 0;
    while ((n = in.read(buffer)) != -1) {
    baos.write(buffer, 0, n);
    }
    imgBytes = baos.toByteArray();

    } catch (Exception ex) {
    ex.printStackTrace();
    } finally {
    if (baos != null) {
    try {
    baos.close();
    } catch (Exception ex) {
    }
    }
    }

    //Here ends the loading part




    //String text1 = "This_is_text_1";

    //String text2 = "This_is_text_2";
    //String text3 = "This_is_text_3";

    //This is my servlet’s URL
    String URL = "http://localhost:18565/TextExampleServlet/ImageServlet";

    HttpConnection http = null;
    OutputStream os = null;
    DataOutputStream dout = null;

    try {
    //Open HttpConnection using POST
    http = (HttpConnection) Connector.open(URL);
    http.setRequestMethod(HttpConnection.POST);
    //Content-Type is must to pass parameters in POST Request
    http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    os = http.openOutputStream();

    // mypart metodo 2
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    dout = new DataOutputStream(bout);

    //dout.writeUTF(imgBytes);

    dout.write(imgBytes);
    //dout.writeUTF(text3);

    os.write(bout.toByteArray());

    os.flush();
    System.out.println("jnnccee...");
    } catch (IOException ex) {
    ex.printStackTrace();
    } //whatever happens, we have to close the streams and connections
    finally {
    if (os != null) {
    try {
    os.close();
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }
    if (dout != null) {
    try {
    dout.close();
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }

    if (http != null) {
    try {
    http.close();
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }
    }
    }
    }).start();
    }

    CODE ENDS

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Converting an image into bytes and writing the bytes into a file in glassfish fol

    Quote Originally Posted by powerohit@gmail.com View Post
    http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    The content you're POSTing isn't URL encoded, so you shouldn't use this content-type. You're sending "image/png".

    Quote Originally Posted by powerohit@gmail.com View Post
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    dout = new DataOutputStream(bout);

    //dout.writeUTF(imgBytes);

    dout.write(imgBytes);
    //dout.writeUTF(text3);

    os.write(bout.toByteArray());
    This fragment is the same as:

    Code:
            os.write(imgBytes);
    I'd suggest you check the response code from the server.

    Graham.

Similar Threads

  1. Convert an muttable image into Array of Bytes
    By nilanchalapanigrahy in forum Mobile Java General
    Replies: 14
    Last Post: 2010-06-19, 10:23
  2. Help on reading and writing into the same CSV File
    By Veron_4 in forum Mobile Java General
    Replies: 9
    Last Post: 2010-01-05, 12:20
  3. Converting Bytes into Date
    By ZaidShah in forum General Development Questions
    Replies: 1
    Last Post: 2009-07-24, 05:15
  4. SOS:accept bytes of a jpg file from the J2EE,but losing some bytes.
    By jerrysdfha in forum Symbian Networking & Messaging (Closed)
    Replies: 1
    Last Post: 2007-01-31, 02:39
  5. Changing images into bytes for storage purpose.
    By okok7878 in forum Mobile Java Media (Graphics & Sounds)
    Replies: 1
    Last Post: 2003-12-21, 22:20

Posting Permissions

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