如何使用库下载文件并打印保存的字节?我尝试使用使用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