在Android中,可以使用Apache Commons Net库将照片上传到FTP服务器。以下是一个简单的实现示例:,,“java,import org.apache.commons.net.ftp.FTP;,import org.apache.commons.net.ftp.FTPClient;,,import java.io.FileInputStream;,import java.io.IOException;,,public class FTPUploader {, public static void uploadFile(String server, int port, String user, String pass, String filePath, String remotePath) {, FTPClient ftpClient = new FTPClient();, try {, ftpClient.connect(server, port);, ftpClient.login(user, pass);, ftpClient.enterLocalPassiveMode();, ftpClient.setFileType(FTP.BINARY_FILE_TYPE);,, FileInputStream inputStream = new FileInputStream(filePath);, boolean done = ftpClient.storeFile(remotePath, inputStream);, inputStream.close();, if (done) {, System.out.println(“The file is uploaded successfully.”);, } else {, System.out.println(“Failed to upload the file.”);, }, } catch (IOException ex) {, ex.printStackTrace();, } finally {, try {, if (ftpClient.isConnected()) {, ftpClient.logout();, ftpClient.disconnect();, }, } catch (IOException ex) {, ex.printStackTrace();, }, }, },},“,,这段代码展示了如何使用Apache Commons Net库连接到FTP服务器并上传文件。请确保在使用前添加相应的依赖项。
发表回复