在Android上使用FTP上载图像

如何在Android上使用FTP上传图片?

Solutions Collecting From Web of "在Android上使用FTP上载图像"

使用SimpleFTP,只需将simpleftp.jar添加到您的类path中,然后导入包中的任何类将使用它:从此处下载

import org.jibble.simpleftp.*; 

确保在上传图片等时使用二进制模式,否则可能会损坏。

 try { SimpleFTP ftp = new SimpleFTP(); // Connect to an FTP server on port 21. ftp.connect("ftp.somewhere.net", 21, "username", "password"); // Set binary mode. ftp.bin(); // Change to a new working directory on the FTP server. ftp.cwd("web"); // Upload some files. ftp.stor(new File("webcam.jpg")); ftp.stor(new File("comicbot-latest.png")); // You can also upload from an InputStream, eg ftp.stor(new FileInputStream(new File("test.png")), "test.png"); ftp.stor(someSocket.getInputStream(), "blah.dat"); // Quit from the FTP server. ftp.disconnect(); } catch (IOException e) { e.printStackTrace(); } 

这是所有的function,所以它不会让你下载文件!

从这里下载FTP Jar库

 public void sendFileViaFTP() { FTPClient ftpClient = null; try { ftpClient = new FTPClient(); ftpClient.connect(InetAddress.getByName("ftp.myserver.com")); if (ftpClient.login("myftpusername", "myftppass")) { ftpClient.enterLocalPassiveMode(); // important! ftpClient.setFileType(FTP.BINARY_FILE_TYPE); String Location = Environment.getExternalStorageDirectory() .toString(); String data = Location + File.separator + "FileToSend.txt"; FileInputStream in = new FileInputStream(new File(data)); boolean result = ftpClient.storeFile("FileToSend.txt", in); in.close(); if (result) Log.v("upload result", "succeeded"); ftpClient.logout(); ftpClient.disconnect(); } } catch (Exception e) { Log.v("count", "error"); e.printStackTrace(); } } 

这将工作肯定。 我做了很多次