安卓开发连接FTP服务器:FTP/SFTP连接
在Android应用程序中,有时需要连接到FTP或SFTP服务器以上传、下载或管理文件,以下是一些关于如何在Android应用中实现FTP和SFTP连接的详细信息。
使用Apache Commons Net库进行FTP连接
要在Android应用程序中实现FTP连接,可以使用Apache Commons Net库,需要在项目的build.gradle
文件中添加以下依赖项:
dependencies { implementation 'commonsnet:commonsnet:3.8.0' }
可以使用以下代码片段建立FTP连接并执行基本操作(如列出目录、上传和下载文件):
import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import java.io.IOException; public class FTPConnection { private String server; private int port; private String user; private String password; private FTPClient ftpClient; public FTPConnection(String server, int port, String user, String password) { this.server = server; this.port = port; this.user = user; this.password = password; } public void connect() throws IOException { ftpClient = new FTPClient(); ftpClient.connect(server, port); ftpClient.login(user, password); ftpClient.enterLocalPassiveMode(); } public void disconnect() throws IOException { if (ftpClient.isConnected()) { ftpClient.logout(); ftpClient.disconnect(); } } public void listFiles() throws IOException { String[] files = ftpClient.listNames(); for (String file : files) { System.out.println(file); } } public void uploadFile(String localPath, String remotePath) throws IOException { ftpClient.storeFile(remotePath, new FileInputStream(localPath)); } public void downloadFile(String remotePath, String localPath) throws IOException { FileOutputStream fos = new FileOutputStream(localPath); ftpClient.retrieveFile(remotePath, fos); fos.close(); } }
使用JSch库进行SFTP连接
要在Android应用程序中实现SFTP连接,可以使用JSch库,需要在项目的build.gradle
文件中添加以下依赖项:
dependencies { implementation 'com.jcraft:jsch:0.1.55' }
可以使用以下代码片段建立SFTP连接并执行基本操作(如列出目录、上传和下载文件):
import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class SFTPConnection { private String host; private int port; private String user; private String password; private ChannelSftp channelSftp; public SFTPConnection(String host, int port, String user, String password) throws Exception { this.host = host; this.port = port; this.user = user; this.password = password; } public void connect() throws Exception { JSch jsch = new JSch(); Session session = jsch.getSession(user, host, port); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); channelSftp = (ChannelSftp) session.openChannel("sftp"); channelSftp.connect(); } public void disconnect() throws Exception { if (channelSftp != null && channelSftp.isConnected()) { channelSftp.disconnect(); } } public void listFiles() throws Exception { Vectorfiles = channelSftp.ls("."); for (ChannelSftp.LsEntry file : files) { System.out.println(file.getFilename()); } } public void uploadFile(String localPath, String remotePath) throws Exception { File file = new File(localPath); InputStream inputStream = new FileInputStream(file); channelSftp.put(inputStream, remotePath); inputStream.close(); } public void downloadFile(String remotePath, String localPath) throws Exception { OutputStream outputStream = new FileOutputStream(localPath); channelSftp.get(remotePath, outputStream); outputStream.close(); } }