getContentLength doesn't work on android for FTP url

隐身守侯 提交于 2020-01-03 03:06:08

问题


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:


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.



来源:https://stackoverflow.com/questions/5897191/getcontentlength-doesnt-work-on-android-for-ftp-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!