开发者

getContentLength doesn't work on android for FTP url

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开发者_如何学Python.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..


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜