I'm trying to upload the file to a server. What is the way for uploading a file to a server through FTP?
Solution from this site
import org.apache.commons.net.ftp.FTPClient;
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(InetAddress.getByName(SERVER));
ftpClient.login(USERNAME, PASSWORD);
ftpClient.changeWorkingDirectory(PATH);
if (ftpClient.getReplyString().contains("250")) {
ftpClient.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
BufferedInputStream buffIn = null;
buffIn = new BufferedInputStream(new FileInputStream(FULL_PATH_TO_LOCAL_FILE));
ftpClient.enterLocalPassiveMode();
ProgressInputStream progressInput = new ProgressInputStream(buffIn, progressHandler);
boolean result = ftpClient.storeFile(localAsset.getFileName(), progressInput);
buffIn.close();
ftpClient.logout();
ftpClient.disconnect();
}
} catch (SocketException e) {
Log.e(SorensonApplication.TAG, e.getStackTrace().toString());
} catch (UnknownHostException e) {
Log.e(SorensonApplication.TAG, e.getStackTrace().toString());
} catch (IOException e) {
Log.e(SorensonApplication.TAG, e.getStackTrace().toString());
}
Add this external jar to your project, and start coding
http://www.jibble.org/simpleftp/
and also check this,
http://www.javabeat.net/tips/36-file-upload-and-download-using-java.html
精彩评论