在我的java应用程序中,我使用以下方法从服务器下载文件.
public void kitapJar(){
File f = new File("C:/PubApp_2.0/update/lib/kitap.jar");
try{
URL kitap = new URL("http://example.com/update/PubApp_2.0.jar");
org.apache.commons.io.FileUtils.copyURLToFile(kitap, f);
}
catch(IOException ex){
System.out.println("Error...!!");}
}
}
Run Code Online (Sandbox Code Playgroud)
但是这个下载非常慢.我怎样才能让它快速?
Hol*_*ger 16
从Java 7开始,您可以下载具有内置功能的文件
Files.copy(
new URL("http://example.com/update/PubApp_2.0.jar").openStream(),
Paths.get("C:/PubApp_2.0/update/lib/kitap.jar"));
// specify StandardCopyOption.REPLACE_EXISTING as 3rd argument to enable overwriting
Run Code Online (Sandbox Code Playgroud)
对于早期版本,从Java 1.4到Java 6的解决方案是
try(
ReadableByteChannel in=Channels.newChannel(
new URL("http://example.com/update/PubApp_2.0.jar").openStream());
FileChannel out=new FileOutputStream(
"C:/PubApp_2.0/update/lib/kitap.jar").getChannel() ) {
out.transferFrom(in, 0, Long.MAX_VALUE);
}
Run Code Online (Sandbox Code Playgroud)
此代码将URL内容传输到没有任何第三方库的文件.如果它仍然很慢,你知道它不是附加库,而且很可能不是Java的错.至少你在这里没有什么可以改进的.那么你应该在JVM之外搜索原因.