java 通过 FTP 将文件从 Android 上传到服务器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7202987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uploading a file from Android to a server through FTP?
提问by Randroid
I'm trying to upload the file to a server. What is the way for uploading a file to a server through FTP?
我正在尝试将文件上传到服务器。通过FTP将文件上传到服务器的方式是什么?
回答by fiction
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());
}
回答by Randroid
Add this external jar to your project, and start coding
将此外部 jar 添加到您的项目中,然后开始编码
http://www.jibble.org/simpleftp/
http://www.jibble.org/simpleftp/
and also check this,
还要检查这个,
http://www.javabeat.net/tips/36-file-upload-and-download-using-java.html
http://www.javabeat.net/tips/36-file-upload-and-download-using-java.html