Reputation: 33
Here is my code.i write this to download mp3 flies,video files & images. i used FileOutputStream for handling files.. All files are downloading well.. mp3 files are working..but images and videos are corrupted
private void download(String fileURL, String destinationDirectory,String name) throws IOException {
// File name that is being downloaded
String downloadedFileName = name;
// Open connection to the file
URL url = new URL(fileURL);
InputStream is = url.openStream();
// Stream to the destionation file
FileOutputStream fos = new FileOutputStream(destinationDirectory + "/" + downloadedFileName);
// Read bytes from URL to the local file
byte[] buffer = new byte[4096];
int bytesRead = 0;
System.out.println("Downloading " + downloadedFileName);
while ((bytesRead = is.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
// Close destination stream
fos.close();
// Close URL stream
is.close();
}
Upvotes: 0
Views: 7095
Reputation: 3913
you need to use BufferedOutputStream.
BufferedOutputStream bos = new BufferedOutputStream(fos );
like this :
private void download(String fileURL, String destinationDirectory,String name) throws IOException {
// File name that is being downloaded
String downloadedFileName = name;
// Open connection to the file
URL url = new URL(fileURL);
InputStream is = url.openStream();
// Stream to the destionation file
FileOutputStream fos = new FileOutputStream(destinationDirectory + "/" + downloadedFileName);
BufferedOutputStream bos = new BufferedOutputStream(fos );
// Read bytes from URL to the local file
byte[] buffer = new byte[4096];
int bytesRead = 0;
System.out.println("Downloading " + downloadedFileName);
while ((bytesRead = is.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush();
// Close destination stream
bos.close();
// Close URL stream
is.close();
}
Upvotes: 0
Reputation: 56
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.
Upvotes: 1
Reputation: 1174
You can try this code,
URLConnection con = new URL(fileURL).openConnection();
InputStream is = con.getInputStream();
OutputStream fos = new FileOutputStream(new File(destinationDirectory + "/" + name));
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) > 0) {
fos.write(buffer, 0, bytesRead);
}
fos.close();
Upvotes: 0
Reputation: 10783
Take a look at such libraries as Apache IO. It has many helper methods such as redirecting streams.
Upvotes: 1