简体   繁体   中英

how to use sun.net.ftp.FtpClient within jdk1.7 on Android platform

I'm new here, and forgive me of my poor English. I want to ask a question about Android and ftp interface.

I use sun.net.ftp.FtpClient correctly in java. But when I copy the code to Android project, it doesn't work. However, the compiler doesn't report error. The error occurs at run-time.

In FTPUtils.java file, I use the following 3 packages:

import sun.net.TelnetOutputStream;  
import sun.net.ftp.FtpClient;  
import sun.net.ftp.FtpProtocolException; 

when I call:

FTPUtils ftpUtils = new FTPUtils();

it crashed.

Here are the stacktrace:

01-25 15:47:03.020: I/dalvikvm(16225): Could not find method sun.net.ftp.FtpClient.close, referenced from method com.cse.yunmixiang.yunapi.FTPUtils.closeConnect
01-25 15:47:03.020: W/dalvikvm(16225): VFY: unable to resolve virtual method 17959: Lsun/net/ftp/FtpClient;.close ()V
01-25 15:47:03.020: D/dalvikvm(16225): VFY: replacing opcode 0x6e at 0x0002
01-25 15:47:03.020: I/dalvikvm(16225): Could not find method sun.net.ftp.FtpClient.create, referenced from method com.cse.yunmixiang.yunapi.FTPUtils.connectServer
01-25 15:47:03.020: W/dalvikvm(16225): VFY: unable to resolve static method 17960: Lsun/net/ftp/FtpClient;.create (Ljava/lang/String;)Lsun/net/ftp/FtpClient;
01-25 15:47:03.020: D/dalvikvm(16225): VFY: replacing opcode 0x71 at 0x0002
01-25 15:47:03.030: I/dalvikvm(16225): Could not find method sun.net.ftp.FtpClient.getFileStream, referenced from method com.example.FTPUtils.getFileSize
...... similier errors ....

There are many functions in stacktrace:

FTPClient.close(); FtpClient.create(); FtpClient.getFileStream(); TelnetOutputStream.close();  Ftp.deleteFile();

I don't know why and how to solve it. Thank you for advance!

It's because Android SDK is not a 1:1 copy of Oracle JDK.

Packages in sun.* are proprietary and they only exist in Oracle's implementation of JVM, they're not a part of the spec and may get dropped from it any minute. Oracle's comment on this

To get FTP on Android you'll have to use a 3rd party lib.

Most common choice is Apache commons-net (no pun intended), another good option is ftp4j

You shouldn't use it on any platform. There is a specific warning in the Javadoc against using classes from the sun.* package. It's there to support FTP URLs used via the URL/URLConnection classes, and that is the only way you should use it. Or use the Apache or other FTP client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM