Java:总是下载档 [英] Java: Download always stalls

查看:135
本文介绍了Java:总是下载档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前,我遇到了一个严重的问题,让我头疼:



所有基于Java的工具(Eclipse,Maven,Android SDK加载程序...)都有下载某些文件的问题:下载只是随机停止。



我已经在超级用户但是没有人能帮我解决问题。因此,我自己开始执行一些测试,最后在这个问题结束时用示例代码。



有趣的部分是缓冲区大小对问题。如果我将缓冲区大小减少到1024,大多数情况下,下载完成。



此代码是否仅在我的Windows系统上出现问题?



要清楚:我不想用Java开发一个下载程序 - 因此修改示例代码并不能帮助我 - 代码只是为了演示问题。

  public static void main(String [] args){
try {
URL url = new URL(http:// mirror .netcologne.de / maven2的/ COM /谷歌/机器人/机器人/ 2.3.3 /机器人-2.3.3.jar);

HttpURLConnection conn =(HttpURLConnection)url.openConnection();
InputStream in = conn.getInputStream();
byte [] buffer = new byte [10485760]; // 10MiB
long read = 0;
while(true){
int bytes = in.read(buffer,0,buffer.length);
if(bytes< 0)
break;
读取+ =字节;
System.out.println(Bytes read:+ read);
}
conn.disconnect();
System.out.println(Finished);
} catch(Exception e){
e.printStackTrace();
}
}


解决方案

您所面临的问题是由Java 7引起的 - 具体来说,给IPv6优先于IPv4。



此问题影响所有基于Java的软件,但仅在某些计算机上发生(可能依赖于使用的Internet连接和/或本地网络组件,如交换机,路由器...)



通过设置系统属性 java.net.preferIPv4Stack = true



设置此属性对于每个应用程序是不同的。对于Eclipse,您必须将其设置在eclipse.ini中。



对于Andoird SDK管理器,您必须编辑文件 tools \android.bat 并添加参数 -Djava.net.preferIPv4Stack = true 到文件末尾附近的Java调用。


Some days ago I run into a severe problem that gives me a headache:

All Java based tools (Eclipse, Maven, Android SDK loader...) have problems downloading certain files: the download just stops at random.

I already described my problem at Superuser but nobody was able to help me with my problem. Therefore started to perform some tests myself and ended up with the sample code at the end of this question.

The interesting part is that the buffer size has an small influence on the problem. If I reduce the buffer size to 1024 the download completes in most cases.

Does this code only make problems on my Windows system?

To make it clear: I don't want to develop a download program with Java - therefore fixing the sample code does not help me - the code is only for demonstrating the problem.

public static void main(String[] args) {
    try {
        URL url = new URL("http://mirror.netcologne.de/maven2/com/google/android/android/2.3.3/android-2.3.3.jar");

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        InputStream in = conn.getInputStream();
        byte[] buffer = new byte[10485760]; // 10MiB
        long read = 0;
        while (true) {
            int bytes = in.read(buffer, 0, buffer.length);
            if (bytes < 0)
                break;
            read += bytes;
            System.out.println("Bytes read: " + read);
        }
        conn.disconnect();
        System.out.println("Finished");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

解决方案

The problem you are facing is caused by Java 7 - in detail that to gives IPv6 a higher priority than IPv4.

This problem affects all Java based software but only occurs on some computers (may depend on the used Internet connection and/or local network components like switches, router...)

You can change it back to IPv4 as it was used in Java 6 by setting the system property java.net.preferIPv4Stack=true

Setting this property is different for each application. For Eclipse you have to set it in the eclipse.ini.

For Andoird SDK manager you have to edit the file tools\android.bat and add the parameter -Djava.net.preferIPv4Stack=true to the Java call near the end of the file.

这篇关于Java:总是下载档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆