I tried your routine. Works fine for me.
I used the URL
"http://www.stephaniequinn.com/Music/Allegro%20from%20Duet%20in%20C%20Major.mp3"
and got a playable MP3 file of exactly 1,430,174 bytes.
Next I tried JPEG:
"http://weknowyourdreams.com/images/beautiful/beautiful-01.jpg"
works fine.
I suspect what happened is that you used URL of a web page instead of the audio/video/pic file by mistake. For example, if you used the URL
"http://weknowyourdreams.com/image.php?pic=/images/beautiful/beautiful-01.jpg"
instead of the one above, you will not get a proper JPG. You'll have to use "View Image" or "Copy Image Location" in your browser.
Answer from Oleksiy Grechnyev on Stack OverflowI tried your routine. Works fine for me.
I used the URL
"http://www.stephaniequinn.com/Music/Allegro%20from%20Duet%20in%20C%20Major.mp3"
and got a playable MP3 file of exactly 1,430,174 bytes.
Next I tried JPEG:
"http://weknowyourdreams.com/images/beautiful/beautiful-01.jpg"
works fine.
I suspect what happened is that you used URL of a web page instead of the audio/video/pic file by mistake. For example, if you used the URL
"http://weknowyourdreams.com/image.php?pic=/images/beautiful/beautiful-01.jpg"
instead of the one above, you will not get a proper JPG. You'll have to use "View Image" or "Copy Image Location" in your browser.
Take a look at such libraries as Apache IO. It has many helper methods such as redirecting streams.
Give Java NIO a try:
URL website = new URL("http://www.website.com/information.asp");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("information.html");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
Using transferFrom() is potentially much more efficient than a simple loop that reads from the source channel and writes to this channel. Many operating systems can transfer bytes directly from the source channel into the filesystem cache without actually copying them.
Check more about it here.
Note: The third parameter in transferFrom is the maximum number of bytes to transfer. Integer.MAX_VALUE will transfer at most 2^31 bytes, Long.MAX_VALUE will allow at most 2^63 bytes (larger than any file in existence).
Use Apache Commons IO. It is just one line of code:
FileUtils.copyURLToFile(URL, File)
None of your code attempts to convert to characters; you're passing bytes through unchanged, so there is no need to worry about encoding. Your code will work fine.
It's only when you use Readers and Writers that you have to worry about encoding.
Assuming that con is an instance of URLConnection, its getInputStream() will provide you a direct network stream reading the bytes as sent by the server. No conversion will be made. Since you are transferring the bytes directly to the file, they are stored in the files without any modification.
Assuming that the server sent the files using the UTF-8 encoding and that the tool you use to open the file afterwards uses the UTF-8 encoding as well, you will see all characters correctly. The same applies to any other encoding, as long as the server and the tool use the same encoding. Your program does not add anything to it as it simply transfers bytes, not characters.
By the way, such a transfer can be made much simpler using recent APIs:
try(ReadableByteChannel in=Channels.newChannel(con.getInputStream());
FileChannel out=FileChannel.open(Paths.get("C:\\programs\\TRYFILE.csv"),
StandardOpenOption.CREATE, StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING)) {
out.transferFrom(in, 0, Long.MAX_VALUE);
}
It gets even more readable when you use import static java.nio.file.StandardOpenOption.*;:
try(ReadableByteChannel in=Channels.newChannel(con.getInputStream());
FileChannel out=FileChannel.open(Paths.get("C:\\programs\\TRYFILE.csv"),
CREATE, WRITE, TRUNCATE_EXISTING) {
out.transferFrom(in, 0, Long.MAX_VALUE);
}