getContentLength doesn't work on android for F

2019-06-07 06:58发布

Following code prints length -1 for filesize on android, but it works fine on desktop JAVA. I'm using Android 2.2.

URL url1 = null;
URLConnection uconn = null;
try {
     url1 = new URL("ftp://FTPHOST/file.zip");
     uconn = url1.openConnection();
     uconn.setDoInput(true);  
     int len= uconn.getContentLength();
     int headersize = uconn.getHeaderFields().size();
     System.out.println("******************************* "+len);
     } catch (Exception e) {     
        e.printStackTrace();
     } 
return null;

Let me know if any workaround in android to get filesize..

1条回答
beautiful°
2楼-- · 2019-06-07 07:17

The Android platform's url connection code uses a different base (Apache HTTP client) under the hood, rather than the Oracle JVM's implementation. Apache HTTP client doesn't natively support FTP download the way the desktop JVM does.

The desktop JVM uses a class that was historically named sun.net.ftp.FtpClient for that FTP functionality. None of the sun classes are available on Android, so that doesn't work. You'll need to get your own FTP client.

查看更多
登录 后发表回答