读书人

ftp文件下传

发布时间: 2012-10-30 16:13:35 作者: rapoo

ftp文件上传
/**
* ftp上传文件 <功能详细描述>
*
* @param filename 本地完整文件名
* @param ftpSite ftp站点信息
* @param remoteFilePath ftp路径
* @throws IOException
* @throws FTPException [参数说明]
*
* @return void [返回类型说明]
* @exception throws [违例类型] [违例说明]
* @see [类、类#方法、类#成员]
*/
public static void upload(String filename, FtpSite ftpSite, String remoteFilePath)
throws IOException, FTPException
{
File file = new File(filename);

// 如果本地的文件存在
if (!file.exists())
{
throw new FileNotFoundException();
}

FTPClient ftp = null;

try
{
// 构造ftp客户端信息
ftp = getFtpClient(ftpSite);

try
{
// 当前文件路径
String pwd = ftp.pwd();

// 检查上传的文件路径是否存在
ftp.chdir(remoteFilePath);

// 回到初始位置
ftp.chdir(pwd);
}
catch (Exception e)
{
// 不存在则创建目录
ftp.mkdir(remoteFilePath);
}

// 上传文件
String remoteFileName = remoteFilePath + "/" + getFileName(filename);

// 上传文件
ftp.put(filename, remoteFileName);
}

finally
{
if (null != ftp)
{
ftp.quit();
}
}
}

读书人网 >软件架构设计

热点推荐