file image uploaded to a webserver appears garbled after upload.

Discussion in 'Java' started by Nishi Bhonsle, Aug 31, 2004.

  1. Hi:

    The images uploaded in a directory on a web server from a client browser, end up garbled. i.e. if the image is uploaded via the UI to some dir say C:\images, after the upload, if i view the image in the destination directory, it looks garbled.
    I verified that the initial image in the source location was not garbled.
    I checked the file size and the permissions on the image at the destination, and it seems to be the same as the one in the source location.
    I also noticed that the garbling does not happen for gif images of size 1K or 2K or 3K but for .jpg images of size > 10 K etc
    (I have not found bigger .gif files to test this.)

    A part of the java code that handles upload creates an object of class MultipartFormHandler (which parses an incoming file upload post).

    This class parses the incoming post and returns MultipartStreamItems for each form item.
    If setCharacterEncoding() is called (with anything other than null), then strings returned from this class will be character-set decoded. Otherwise, strings returned from this class are not character-set decoded. In this way, MultipartFormHandler precisely matches the behavior of ServletRequest.


    MultipartFormHandler handler = new MultipartFormHandler(request);
    handler.setCharacterEncoding("UTF-8");

    if (imageFile != null)
    {
    FileReader reader = new FileReader(file);
    FileWriter writer = new FileWriter(imageFile);
    int c;
    while ((c=reader.read()) >= 0)
    {
    writer.write(c);
    }
    reader.close();
    writer.close();
    }

    What can cause the garble?

    Thanks, Nishi.
    Nishi Bhonsle, Aug 31, 2004
    #1
    1. Advertising

  2. Nishi Bhonsle

    Paul Lutus Guest

    Nishi Bhonsle wrote:

    >
    > Hi:
    >
    > The images uploaded in a directory on a web server from a client browser,
    > end up garbled.



    From the documentation:

    "FileReader is meant for reading streams of characters. For reading streams
    of raw bytes, consider using a FileInputStream."


    --
    Paul Lutus
    http://www.arachnoid.com
    Paul Lutus, Sep 1, 2004
    #2
    1. Advertising

  3. On Tue, 31 Aug 2004 14:26:29 -0700, Nishi Bhonsle
    <> wrote:

    >
    > Hi:
    >
    > The images uploaded in a directory on a web server from a client
    > browser, end up garbled. i.e. if the image is uploaded via the UI to
    > some dir say C:\images, after the upload, if i view the image in the
    > destination directory, it looks garbled.
    > I verified that the initial image in the source location was not garbled.
    > I checked the file size and the permissions on the image at the
    > destination, and it seems to be the same as the one in the source
    > location.
    > I also noticed that the garbling does not happen for gif images of size
    > 1K or 2K or 3K but for .jpg images of size > 10 K etc
    > (I have not found bigger .gif files to test this.)
    >
    > A part of the java code that handles upload creates an object of class
    > MultipartFormHandler (which parses an incoming file upload post).
    >
    > This class parses the incoming post and returns MultipartStreamItems for
    > each form item.
    > If setCharacterEncoding() is called (with anything other than null),
    > then strings returned from this class will be character-set decoded.
    > Otherwise, strings returned from this class are not character-set
    > decoded. In this way, MultipartFormHandler precisely matches the
    > behavior of ServletRequest.
    >
    >
    > MultipartFormHandler handler = new MultipartFormHandler(request);
    > handler.setCharacterEncoding("UTF-8");
    >
    > if (imageFile != null)
    > {
    > FileReader reader = new FileReader(file);
    > FileWriter writer = new FileWriter(imageFile);


    ****** Bingo - A FileReader tries to turn a byte stream into characters
    A FileWriter does a character conversion on writing.

    You should be using plain InputStream etc. for manipulating bytes.




    > int c;
    > while ((c=reader.read()) >= 0)
    > {
    > writer.write(c);
    > }
    > reader.close();
    > writer.close();
    > }
    >
    > What can cause the garble?
    >
    > Thanks, Nishi.
    >




    --
    Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
    William Brogden, Sep 1, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt G
    Replies:
    1
    Views:
    1,148
    Deepak Kumar Vasudevan
    Aug 22, 2003
  2. Long Le
    Replies:
    3
    Views:
    1,168
    Long Le
    Aug 11, 2004
  3. =?Utf-8?B?SWJyYWhpbS4=?=

    File name is displayed Garbled in "File download dialog box"

    =?Utf-8?B?SWJyYWhpbS4=?=, Aug 12, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    482
    =?Utf-8?B?SWJyYWhpbS4=?=
    Aug 12, 2006
  4. blbmdsmith
    Replies:
    1
    Views:
    643
    Graham Dumpleton
    Dec 14, 2006
  5. Byron
    Replies:
    4
    Views:
    129
    Thomas 'PointedEars' Lahn
    Jul 17, 2005
Loading...

Share This Page