Solved

URLConnection Binary Transfer

Posted on 2003-10-25
2
412 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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

688 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