Solved

URLConnection Binary Transfer

Posted on 2003-10-25
2
410 Views
Last Modified: 2008-01-09
Basically I'm looking to transfer files from a URLConnection object to a file.  Although ASCII transfers fine over this method I get a few off characters in transferring a binary file (ie: a jpg image) giving the following output...

C:\>comp file1.jpg file1real.jpg
Compare error at OFFSET 123
file1 = 3F
file2 = 81
Compare error at OFFSET 581
file1 = 3F
file2 = 8F
Compare error at OFFSET 58E
file1 = 3F
file2 = 8F
Compare error at OFFSET 59B
file1 = 3F
file2 = 8F
Compare error at OFFSET 5A0
file1 = 3F
file2 = 81
Compare error at OFFSET 656
file1 = 3F
file2 = 90
Compare error at OFFSET 666
file1 = 3F
file2 = 8F
Compare error at OFFSET 7D9
file1 = 3F
file2 = 81
Compare error at OFFSET AE9
file1 = 3F
file2 = 81
Compare error at OFFSET BC7
file1 = 3F
file2 = 9D
10 mismatches - ending compare

from this code... (minus try/catch and whatnot)

        URL site = new URL(jTextField1.getText());
        URLConnection sock = site.openConnection();
        FileWriter fd = new FileWriter(jTextField2.getText());
        BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
        for (int i = 0; i < sock.getContentLength(); i++)
            fd.write(in.read());
        fd.close();
        in.close();

Essentially I figured since in.read() and fd.write() return and accept an int argument that this would be more than enough space for a byte field and therefore wouldn't drop any data.  Any ideas?
-Graham
0
Comment
Question by:halodev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 9620497
You can't use Reader/Writer for binary files. Use BufferedInputStream and BufferedOutputStream
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9620787
:-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question