public boolean sendFile(final String hostname, final String username, final String password, final String remotePath,
final File file, final String destinationFileName)
{
boolean allDone = false;
FTPClient ftpClient = new FTPClient();
try
{
int reply;
ftpClient.connect(hostname);
reply = ftpClient.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply))
{
throw new RuntimeException("Unable to connect to ftp server");
}
if(!ftpClient.login(username, password))
{
throw new RuntimeException("Unable to login to ftp server");
}
if(!ftpClient.changeWorkingDirectory(remotePath))
{
throw new RuntimeException("Unable to change working directory");
}
if(!ftpClient.setFileType(FTP.ASCII_FILE_TYPE))
{
throw new RuntimeException("Unable to set file mode ");
}
String tempFileName = destinationFileName + FILEEXT_TEMP;
FileInputStream fis = new FileInputStream(file);
try
{
ftpClient.storeFile(tempFileName, fis);
fis.close();
}
finally
{
fis.close();
}
String finalFileName = destinationFileName;
if(!ftpClient.rename(tempFileName, finalFileName))
{
if(!ftpClient.deleteFile(tempFileName))
{
LOG.warn("Unable to delete temp file");
}
throw new RuntimeException("Unable to rename file");
}
ftpClient.logout();
allDone = true;
}
catch (Exception e)
{
LOG.fatal("Unable to FTP file", e);
}
finally
{
if(ftpClient.isConnected())
{
try
{
ftpClient.disconnect();
}
catch (IOException e)
{
LOG.warn("Erro while disconnecting from FTP", e);
}
}
}
return allDone;
}
FTP source code
原创
©著作权归作者所有:来自51CTO博客作者sunxboy的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
clojure source code
11
github -
Build Android Source Code
Setting up your machine To build the Android source files,
android git java linux ide -
Source code manager common
TFS2013安装教程TFS2010安装教程
安装教程 版本控制 svn