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();
}
}
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();
}
}
相关文章推荐
- apache FtpClient上传下载删除文件夹及文件
- java 中 FtpClient 实现 FTP 文件上传、下载
- JAVA中使用FTPClient实现文件上传下载
- C# FTPClient--FTP操作帮助类,上传下载,文件,目录操作
- 利用webclient ftpclient上传下载文件
- FTPClientHelper辅助类 实现文件上传,目录操作,下载等操作
- Java FTPClient实现文件上传下载
- C# FTPClient--FTP操作帮助类,上传下载,文件,目录操作
- Apache FTPClient操作文件上传下载及公共类
- JAVA中使用FTPClient实现文件上传下载(键人岐)
- Apache FTPClient操作文件上传下载及公共类
- 利用FtpClient实现上传下载及获得文件目录
- org.apache.commons.net.ftp.FTPClient文件上传下载,中文乱码
- 关于用FTPClient资源库上传下载以中文命名的文件的问题
- Java代码使用FTPClient实现ftp文件上传下载和删除的功能
- Apache FTPClient操作文件上传下载及公共类
- FTP文件的上传和下载---org.apache.commons.net.ftp.FTPClient
- 【C#公共帮助类】FTPClientHelper帮助类,实现文件上传,目录操作,下载等动作
- Apache FTPClient上传下载文件
- 用FTPClient对ftp文件进行上传下载等操作