android ftp上传文件
时间: 2023-10-27 14:08:23 浏览: 67
可以使用Apache Commons Net库来实现Android FTP上传文件。以下是一个简单的示例代码:
```java
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FTPUploader {
private FTPClient ftpClient;
public FTPUploader() {
ftpClient = new FTPClient();
}
public void uploadFile(String server, int port, String user, String password, String filePath, String remoteDir) throws IOException {
ftpClient.connect(server, port);
ftpClient.login(user, password);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
File localFile = new File(filePath);
FileInputStream inputStream = new FileInputStream(localFile);
String remoteFilePath = remoteDir + "/" + localFile.getName();
boolean done = ftpClient.storeFile(remoteFilePath, inputStream);
inputStream.close();
if (done) {
System.out.println("The file was uploaded successfully.");
} else {
System.out.println("Could not upload the file.");
}
ftpClient.logout();
ftpClient.disconnect();
}
}
```
使用示例:
```java
FTPUploader uploader = new FTPUploader();
uploader.uploadFile("ftp.example.com", 21, "username", "password", "/path/to/local/file", "/path/to/remote/directory");
```