如何使用庫下載文件並打印保存的字節?我嘗試使用使用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);
}
}
我將如何擴展Apache來使用它? 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( 「內容長度」); IOUtils.copy(is,os); //開始傳輸 我在做對吧? – Kyle
你介意把上面的代碼附加到你的問題上嗎?這很難閱讀。我會盡力幫助。 – gigadot
我加了代碼謝謝你的幫助。 – Kyle