2016-09-09 924 views
0

这里是我的code.i写这个下载MP3苍蝇,视频文件&图像。 我用FileOutputStream中处理文件.. 的所有文件都下载好.. MP3文件working..but图像和视频被破坏用java下载文件 - 文件损坏

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(); 
    } 
+0

此代码应做工精细...但是你应该学会打开和关闭正确的资源。特别是使用try-with-resources语句。 – fge

回答

1

我尝试了你的日常工作。对我来说工作得很好。

我使用的URL

http://www.stephaniequinn.com/Music/Allegro%20from%20Duet%20in%20C%20Major.mp3

,并得到确切的字节数1430174可播放MP3文件。

下一个我试图JPEG:

http://weknowyourdreams.com/images/beautiful/beautiful-01.jpg

工作正常。

我怀疑发生了什么是您错误地使用了网页的URL而不是音频/视频/图片文件。例如,如果您使用的URL

http://weknowyourdreams.com/image.php?pic=/images/beautiful/beautiful-01.jpg

,而不是一个上面,你就不会得到适当的JPG。您必须在浏览器中使用“查看图像”或“复制图像位置”。

+0

此代码适用于mp3..but图像和视频已损坏 –

+0

它是否与“http://weknowyourdreams.com/images/beautiful/beautiful-01.jpg”一起使用? –

+0

基本上你的例程应该可以工作,但我会用try-with-resources而不是close()和Buffered流。缓冲流是高效的,你可以用一个简单的 int c; ((c = is.read())!= - 1)fos.write(c); 没有打扰创建你自己的缓冲区。 –

0

你可以试试这个代码,

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(); 
+0

这与问题中的代码有什么不同? – Henry

+0

在流中有一个传送... –

0

你需要使用的BufferedOutputStream。

BufferedOutputStream bos = new BufferedOutputStream(fos); 

这样的:

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(); 
    } 
+0

“你需要冲洗”:不,你不需要。 – Henry

+0

你是对的,OutputStream的flush方法什么都不做。 –

+0

无需刷新..代码运行良好 –