Java实现FTP上传和下载


import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

/**
* <p>
* Title: Adage_[子系统统名]_[模块名]
* </p>
* <p>
* Description: [ftp上传下载文件]
* </p>
*
* @author benDaoRong
* @version $Revision$ 2014-8-14
* @author (lastest modification by $Author$)
* @since 1.0
*/
public class FtpTest {
/**
* <p>
* Discription:[上传文件到ftp服务器]
* </p>
*
* @return
*/
public static boolean upload() {
String url = "10.1.50.111";
String username = "test";
String password = "test";

FTPClient client = new FTPClient();
try {
client.connect(url);
client.login(username, password);
int reply = client.getReplyCode();
System.out.println("reoktCode:" + reply);
//
if (!FTPReply.isPositiveCompletion(reply)) {
client.disconnect();
return false;
}
client.changeWorkingDirectory("F:/FTPROOT");

// 上传文件
// FileInputStream fileInput = new FileInputStream("F:/Desktop/620.txt");
// System.out.println("文件上传中,请耐心等待!");
// client.storeFile(new String("620.txt".getBytes("UTF-8"), "iso-8859-1"), fileInput);
// System.out.println("文件上传完成");
// 在ftp服务器上新建一个文件,然后将输入流写入文件。
InputStream input = new ByteArrayInputStream("今天测试ftp很成功111".getBytes());
client.storeFile(new String("中文名2.txt".getBytes("UTF-8"), "iso-8859-1"), input);
input.close();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (client.isConnected()) {
try {
client.disconnect();
System.out.println("关闭ftp连接");
}
catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}

/**
* <p>
* Discription:[从ftp服务器下载文件]
* </p>
*/
public static void download() {
String url = "10.1.50.111";
String username = "test";
String password = "test";
FTPClient client = new FTPClient();
try {
client.connect(url);
client.login(username, password);
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
client.disconnect();
}
// 设置ftp根目录
client.changeWorkingDirectory("F:/FTPROOT");
// 下载文件
FTPFile[] files = client.listFiles();
if (files != null && files.length > 0) {
for (FTPFile file : files) {
String fileName = new String(file.getName().getBytes("iso-8859-1"), "UTF-8");
// 本地文件存放目录
String localName = System.currentTimeMillis() + "_" + fileName;
OutputStream out = new FileOutputStream(new File("F:/Desktop/" + localName));
client.retrieveFile(file.getName(), out);
out.close();
System.out.println("文件[" + fileName + "]下载成功");
}
}

client.logout();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (client.isConnected()) {
try {
client.disconnect();
}
catch (IOException e) {
e.printStackTrace();
}
}
}

}

public static void main(String[] args) {
// upload();
download();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值