有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

apachecommons如何下载java文件?

我尝试了一些字节while循环方法,下面是这个方法:

try {
     URL dl = null;
     dl = new URL("http://ds-forums.com/kyle-tests/uploads/Screenshots.zip");
     ReadableByteChannel rbc = Channels.newChannel(dl.openStream());
     FileOutputStream fos = new FileOutputStream(fileName + "Screenshots.zip");
     fos.getChannel().transferFrom(rbc, 0, 1 << 24);
     System.out.println(fos.getChannel().size());
     fos.close();
     rbc.close();
 } catch (Exception e) {
     e.printStackTrace();
 }

}

但是这些方法不是很有效/快速。我发现了ApacheUTIL,我正在使用

 IOUtils.copy(new URL("http://ds-forums.com/kyle-tests/uploads/Screenshots.zip").openStream(), new FileOutputStream(System.getProperty("user.home").replace("\\", "/") + "/Desktop/Screenshots.zip"));

但这是最好的方法吗?我现在很困惑哪种方法最适合下载26mb的压缩文件。(上面的文件只有1mb,我正在测试方法)

我只是想看看是否有人遇到过这个问题,也许他们可以帮助我。谢谢


共 (1) 个答案

  1. # 1 楼答案

    如果类路径上已经有Commons IO,请使用

    org.apache.commons.io.FileUtils.copyURLToFile(URL, File)
    

    它负责打开、关闭和调用文件父级上的mkdirs的所有流管理