这是我的代码,我写这个是为了下载mp3苍蝇,视频文件和图像。我用FileOutputStream来处理文件。所有文件都下载得很好。mp3文件是working..but图像,视频被破坏
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();
}发布于 2016-09-09 09:39:21
我试过你的惯例了。对我来说很好。
我使用了URL
"http://www.stephaniequinn.com/Music/Allegro%20from%20Duet%20in%20C%20Major.mp3“
得到一个可播放的MP3文件,精确地是1,430,174字节。
接下来,我尝试了JPEG:
"http://weknowyourdreams.com/images/beautiful/beautiful-01.jpg“
效果很好。
我怀疑所发生的事情是你错误地使用了一个网页的URL而不是音频/视频/图片文件。例如,如果您使用了URL
"http://weknowyourdreams.com/image.php?pic=/images/beautiful/beautiful-01.jpg“
而不是上面的,您将不会得到一个适当的JPG。您必须在浏览器中使用“查看图像”或“复制图像位置”。
发布于 2016-09-09 09:40:51
你可以试试这个代码,
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();https://stackoverflow.com/questions/39408127
复制相似问题