2016-09-09 6 views
0

ここに私のcode.iはmp3ファイル、ビデオファイル&のイメージをダウンロードするために書き込みます。 は、私にはよく すべてのファイルがダウンロードされている。..ファイルを処理するためのFileOutputStreamを使用.. mp3ファイルはworking..but画像や動画ありApache IOなどのライブラリを見てみましょう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

あなたのルーチンを試しました。私のためにうまく動作します。

私は

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

URLを使用して正確に1430174バイトの再生可能なMP3ファイルを得ました。

次私はJPEGを試してみました:

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

が正常に動作します。

私は誤ってaudio/video/picファイルの代わりにWebページのURLを使用したと思われます。あなたはURL

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

の代わりに、上記のいずれかを使用している場合たとえば、あなたが適切なJPGを取得することはありません。ブラウザで「画像の表示」または「画像の場所のコピー」を使用する必要があります。

+0

このコードはmp3で動作しますが、画像とビデオが壊れています –

+0

「http://weknowyourdreams.com/images/beautiful/beautiful-01.jpg」と連携して動作しますか? –

+0

基本的にあなたのルーチンはうまくいくはずですが、close()とBuffered streamsの代わりにtry-with-resourcesを使用します。バッファされたストリームは効率的で、単純なものでコピーすることができます int c; while((c = is.read())!= - 1)fos.write(c); 独自のバッファを作成する必要はありません。 –

1

を破損しています。 redirecting streamsのようなヘルパーメソッドがたくさんあります。

0

あなたはなBufferedOutputStreamを使用する必要が

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 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

コードを正常に動作させる必要はありません –

関連する問題