2011-01-15 6 views
13

ライブラリを使用してファイルをダウンロードし、保存されたバイトをプリントアウトするにはどうすればよいですか?私は使用しようとしましたjava apache commonsを使用してファイルをダウンロードしますか?

import static org.apache.commons.io.FileUtils.copyURLToFile; 
public static void Download() { 

     URL dl = null; 
     File fl = null; 
     try { 
      fl = new File(System.getProperty("user.home").replace("\\", "/") + "/Desktop/Screenshots.zip"); 
      dl = new URL("http://ds-forums.com/kyle-tests/uploads/Screenshots.zip"); 
      copyURLToFile(dl, fl); 
     } catch (Exception e) { 
      System.out.println(e); 
     } 
    } 

しかし、私はバイトまたはプログレスバーを表示することはできません。どちらの方法を使うべきですか?

public class download { 
    public static void Download() { 
     URL dl = null; 
     File fl = null; 
     String x = null; 
     try { 
      fl = new File(System.getProperty("user.home").replace("\\", "/") + "/Desktop/Screenshots.zip"); 
      dl = new URL("http://ds-forums.com/kyle-tests/uploads/Screenshots.zip"); 
      OutputStream os = new FileOutputStream(fl); 
      InputStream is = dl.openStream(); 
      CountingOutputStream count = new CountingOutputStream(os); 
      dl.openConnection().getHeaderField("Content-Length"); 
      IOUtils.copy(is, os);//begin transfer 

      os.close();//close streams 
      is.close();//^ 
     } catch (Exception e) { 
      System.out.println(e); 
     } 
    } 

答えて

13

ダウンロードする前に合計バイト数を取得する方法をお探しの場合は、http応答のContent-Lengthヘッダーからこの値を取得できます。

ダウンロードした後の最終バイト数を知りたい場合は、書き込み先のファイルサイズを確認するのが一番簡単です。

あなたがダウンロードされたバイト数の現在の進行状況を表示したい場合は、あなたがFileOutputStreamのように毎回write方法は、それが通過するバイト数と更新を数えると呼ばれているラップするようにApache CountingOutputStreamを拡張したい場合があります進捗バー。ここで

更新

DownloadCountingOutputStreamの単純な実装です。 ActionListenerを使い慣れているかどうかはわかりませんが、GUIを実装するのに便利なクラスです。

public class DownloadCountingOutputStream extends CountingOutputStream { 

    private ActionListener listener = null; 

    public DownloadCountingOutputStream(OutputStream out) { 
     super(out); 
    } 

    public void setListener(ActionListener listener) { 
     this.listener = listener; 
    } 

    @Override 
    protected void afterWrite(int n) throws IOException { 
     super.afterWrite(n); 
     if (listener != null) { 
      listener.actionPerformed(new ActionEvent(this, 0, null)); 
     } 
    } 

} 

これは、使用サンプルです:

public class Downloader { 

    private static class ProgressListener implements ActionListener { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      // e.getSource() gives you the object of DownloadCountingOutputStream 
      // because you set it in the overriden method, afterWrite(). 
      System.out.println("Downloaded bytes : " + ((DownloadCountingOutputStream) e.getSource()).getByteCount()); 
     } 
    } 

    public static void main(String[] args) { 
     URL dl = null; 
     File fl = null; 
     String x = null; 
     OutputStream os = null; 
     InputStream is = null; 
     ProgressListener progressListener = new ProgressListener(); 
     try { 
      fl = new File(System.getProperty("user.home").replace("\\", "/") + "/Desktop/Screenshots.zip"); 
      dl = new URL("http://ds-forums.com/kyle-tests/uploads/Screenshots.zip"); 
      os = new FileOutputStream(fl); 
      is = dl.openStream(); 

      DownloadCountingOutputStream dcount = new DownloadCountingOutputStream(os); 
      dcount.setListener(progressListener); 

      // this line give you the total length of source stream as a String. 
      // you may want to convert to integer and store this value to 
      // calculate percentage of the progression. 
      dl.openConnection().getHeaderField("Content-Length"); 

      // begin transfer by writing to dcount, not os. 
      IOUtils.copy(is, dcount); 

     } catch (Exception e) { 
      System.out.println(e); 
     } finally { 
      IOUtils.closeQuietly(os); 
      IOUtils.closeQuietly(is); 
     } 
    } 
} 
+0

私はそれを使うためにどのようにApacheを拡張するのですか? fl = new File(System.getProperty( "user.home")。replace( "\\"、 "/")+ "/Desktop/Scr​​eenshots.zip"); dl =新しいURL( "http://ds-forums.com/kyle-tests/uploads/Screenshots.zip"); OutputStream os =新しいFileOutputStream(fl); InputStream is = dl.openStream(); CountingOutputStream count =新しいCountingOutputStream(os); dl.openConnection()。getHeaderField( "Content-Length"); IOUtils.copy(is、os); //転送を開始します 私はそれを正しくやっていますか? – Kyle

+0

あなたの質問に上記のコードを追加してもよろしいですか?読むのは難しいです。私は助けようとします。 – gigadot

+0

コードを追加していただきありがとうございます。 – Kyle

10

commons-ioIOUtils.copy(inputStream, outputStream)である。したがって:

OutputStream os = new FileOutputStream(fl); 
InputStream is = dl.openStream(); 

IOUtils.copy(is, os); 

IOUtils.toByteArray(is)を使用してバイトを取得できます。

合計バイト数の取得は、別の話です。ストリームはあなたに合計を与えるものではありません。ストリームで現在利用可能なものだけを提供できます。しかしそれが流れなので、それはもっと来ることができます。

そのため、httpには合計バイト数を指定する特殊な方法があります。応答ヘッダContent-Lengthにあります。だから、url.openConnection()に電話して、URLConnectionオブジェクトでgetHeaderField("Content-Length")に電話する必要があります。文字列としてバイト数を返します。その後、Integer.parseInt(bytesString)を使用すると、合計が得られます。

+0

うーん...そのストリームからダウンロードされたバイトを表示する方法はありますか?私は見ているが、私は方法を見ていない。返信いただきありがとうございます。 – Kyle

+0

'IOUtils.toByteArray(..)' – Bozho

+0

btw、バイト自体、またはそれらのカウント? – Bozho

関連する問題