Link to home
Start Free TrialLog in
Avatar of halodev
halodev

asked on

URLConnection Binary Transfer

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
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:-)