我正在尝试将文件上传到服务器。通过FTP将文件上传到服务器的方法是什么?
答案 0 :(得分:10)
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());
}
答案 1 :(得分:5)
将此外部jar添加到项目中,然后开始编码
http://www.jibble.org/simpleftp/
并检查一下,
http://www.javabeat.net/tips/36-file-upload-and-download-using-java.html