www.webdeveloper.com
Results 1 to 7 of 7

Thread: Scrambled images when downloading across HttpURLConnection

  1. #1
    Join Date
    Dec 2004
    Posts
    17

    Question Scrambled images when downloading across HttpURLConnection

    When attempting a .gif download from an URL address with an http protocol, the file is the right size, however the image is not loadable when viewing in a windows appliaction. Below is a bit of the sample code being used:

    HttpURLConnection imgConnection = (HttpURLConnection fileName.openConnection(); // filename being the http URL
    imgConnection.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)");
    InputStreamReader fileIn = new InputStreamReader(imgConnection.getInputStream());
    FileWriter fileOut = new FileWriter(newName); // newName being client side filename
    int c = 0;
    while ((c = fileIn.read()) != -1) {
    fileOut.write(c);
    }
    fileOut.flush();
    fileOut.close();
    fileIn.close();

    Is there a better way? Maybe some kind of URL Encoding issue? Please help.

  2. #2
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    It looks to me like you're not parsing the GIF image data out of the response. Open the file up in a text editor and I think you'll see the HTTP headers as the first few lines.

  3. #3
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,120
    you will need to parse the image from the http stream.
    1. make sure that you set the
    enctype="multipart/form-data"
    then parse it http stream and strip the image from it.
    Then I think you can create an image and save that image to the file...
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  4. #4
    Join Date
    Dec 2004
    Posts
    17
    Thanks all.

    Actually I took a look at the binary image in text format and I did not see any html headers in the file.

    I also added a the line:

    imgConnection.addRequestProperty("Enctype", "multipart/form-data");

    Is this proper way to add the request property?

    It stll seems to be scrambled. Any ideas?

  5. #5
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    I don't know about that encoding for a request but I'm a bit leery of it. Are the first 3 bytes of the downloaded image "GIF"? If not then does "GIF" appear anywhere in the image?

  6. #6
    Join Date
    Dec 2004
    Posts
    17

    Thumbs up Problem solved

    After changing instances of InputStreamReader to BufferedInputStream, as well as changing FileWriter to BufferedOutputStream, all is working properly.

    It turns out that Reader and Writer classes cannot handle binary data very well.

    Thanks to all those that helped with this problem.

  7. #7
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    Ahh. The Readers and Writers may do end-of-line conversions ala FTP in text mode.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles