您的位置:首页 > 编程语言 > PHP开发

FTPClient上传下载文件

2013-07-03 17:12 453 查看
需要com.springsource.org.apache.commons.net-1.4.1.jar和com.springsource.org.apache.oro-2.0.8.jar两个FTPClient的包

public static void main(String[] args) {

//上传文件

FTPClient ftp = new FTPClient();

try {

int reply;

//url FTP服务器hostname ,port FTP服务器端口

ftp.connect("172.0.0.1", 21);//连接FTP服务器

//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器

//创建输入流

FileInputStream input = new FileInputStream("D:\\apache-tomcat-6.0.18\\webapps\\FTDMProject\\upload\\20130703\\test.txt");

//username FTP登录账号、密码

ftp.login("sa", "sa");//登录

reply = ftp.getReplyCode();

if (!FTPReply.isPositiveCompletion(reply)) {

ftp.disconnect();

}

//FTP服务器保存目录

ftp.changeWorkingDirectory("/ProjectMaterial/file/2013/7/3");

//上传到FTP服务器上的文件名,输入流

ftp.storeFile("test", input);

input.close();

ftp.logout();

System.out.println("ok");

} catch (IOException e) {

e.printStackTrace();

} finally {

if (ftp.isConnected()) {

try {

ftp.disconnect();

} catch (IOException ioe) {

}

}

}

}

public static void main(String[] args) {

//下载文件

try{

int port = 21;

FTPClient ftpClient = new FTPClient();

ftpClient.connect(ftpServer, port);

ftpClient.login(loginName, password);

//FTP服务器保存目录

ftpClient.changeWorkingDirectory(filePath);

/创建/输出流

FileOutputStream fos = new FileOutputStream(downfile+fileName);

//文件名,输出流

ftpClient.retrieveFile(fileName, fos);

fos.flush();

fos.close();

}catch(IOException e){

e.printStackTrace();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: