İndirmeden önce toplam bayt sayısını elde etmenin bir yolunu arıyorsanız, bu değeri http yanıtında Content-Length
başlığından edinebilirsiniz.
İndirmeden sonra yalnızca son bayt sayısını istiyorsanız, yazdığınız dosya boyutunu kontrol etmek en kolay yoldur. Eğer indirilmiş olan kaç bayt mevcut ilerlemesini görüntülemek istiyorsanız
Ancak, bunu write
yöntemleri denir ki her geçen bayt ve güncelleme sayar FileOutputStream
sarmak için apache CountingOutputStream
uzatmak isteyebilirsiniz ilerleme çubuğu.
Güncelleme İşte
DownloadCountingOutputStream
basit bir uygulamasıdır. ActionListener
'u kullanmaya alışkın olup olmadığınızdan emin değilim, ancak GUI'yi uygulamak için kullanışlı bir sınıf.
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));
}
}
}
Bu kullanım örneğidir:
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);
}
}
}
Bunu kullanmak için apache'yi nasıl uzatabilirim? fl = new File (System.getProperty ("user.home"). değiştirin ("\\", "/") + "/Desktop/Screenshots.zip"); dl = yeni URL ("http://ds-forums.com/kyle-tests/uploads/Screenshots.zip"); OutputStream os = yeni FileOutputStream (fl); InputStream is = dl.openStream(); CountingOutputStream count = new CountingOutputStream (os); dl.openConnection().getHeaderField ("İçerik-Uzunluk"); IOUtils.copy (is, os); // transfer işlemini başlat Doğru mu yapıyorum? – Kyle
Yukarıdaki kodu sorunuza ekleyebilir misiniz? Okuması zor. Yardım etmeye çalışacağım. – gigadot
Kodu ekledim yardım için teşekkür ederim. – Kyle