Solved

URLConnection Binary Transfer

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

758 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now