FTP上传和下载

现今FTP上传和下载应用广泛,很多银行和保险公司等都在使用,FTP上传和下载方法也有很多,下面列出一种常用的也是我实现过的方式。

第一步,连接FTP;

 //连接ftp服务器
private boolean connectServer(String ip, String user, String password,
String path) throws IOException {


// server:FTP服务器的IP地址;user:登录FTP服务器的用户名;password:登录FTP服务器的用户名的口令;path:FTP服务器上的
// 路径
try {
ftpClient = new FtpClient();
ftpClient.openServer(ip);


ftpClient.login(user, password);


if (path.length() != 0) { // path是ftp服务下主目录的子目录
ftpClient.cd(path);
}
ftpClient.binary(); // 用2进制上传
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}

第二步创建目标目录(这是为上传做准备,如果有此目录就不会创建,没有才会创建,如果是下载就没必要了)

/**
* 创建目标目录
* @param dir
* @return
*/
public  boolean createDir(String dir) {
try {
connectServer(server, user, password, path);
ftpClient.ascii();
StringTokenizer s = new StringTokenizer(dir, "/"); // sign
s.countTokens();
String pathName = ftpClient.pwd();
while (s.hasMoreElements()) {
pathName = pathName + "/" + (String) s.nextElement();
try {
ftpClient.sendServer("MKD " + pathName + "\r\n");
} catch (Exception e) {
e = null;
return false;
}
ftpClient.readServerResponse();
}
ftpClient.binary();
return true;
} catch (IOException e1) {
e1.printStackTrace();
return false;
}
}

第三步上传或者下载

上传:

/**
* 上传到指定的FTP服务器
* @param vtsFilePath
* @param strEdorAcceptNo
* @return
* @throws Exception
*/
public boolean uploadFile(String vtsFilePath, String strEdorAcceptNo,String uploadPath)
throws Exception {
if (server == null || "".equals(server)) {
return false;
}


TelnetOutputStream os = null;
BufferedInputStream bis = null;
try {
// 连接ftp服务器
ftpClient.openServer(server);
ftpClient.login(user, password);
// path是ftp服务下主目录的子目录
path=path+"/"+uploadPath;
if (path != null && path.length() != 0) {
try {
ftpClient.cd(path);
} catch (Exception e) {
if(createDir(path)){
ftpClient.cd(path);
}
}

}
// 用2进制上传
ftpClient.binary();
try {
File file = new File(vtsFilePath);
FileInputStream fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
if (bis == null) {
return false;
}
// 用ftp上传后的新文件名
System.out.println(strEdorAcceptNo);
os = ftpClient.put(strEdorAcceptNo);
byte[] bytes = new byte[1024];
int c;
while ((c = bis.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
bis.close();
os.close();
logger.debug("文件上传成功!");
// }
} catch (IOException e) {
// ExcAct.doHandle(e);
logger.debug("上传文件失败!");
e.printStackTrace();
return false;
}
finally {
if (bis != null) {
bis.close();
}
if (os != null) {
os.close();
}
}
} catch (IOException e) {
if (e instanceof java.net.UnknownHostException) {
logger.debug("您输入的IP地址无法解析!");
} else if (e instanceof sun.net.ftp.FtpLoginException) {
logger.debug("您输入的用户名或密码不正确!");
} else if (e instanceof java.io.FileNotFoundException) {
logger.debug("您输入的文件路径不存在!");
} else {
logger.debug(e.getMessage());
}
return false;
}
finally {
try {
if (ftpClient != null) {
ftpClient.closeServer();
}
} catch (IOException e) {
logger.debug("关闭与ftp服务器的连接时失败!");
return false;
}
}
return true;
}

下载:

 /**
  * FTP文件下载
  * @param savePath 存放文件的地址(本地)
  * @param fileName 要获取的文件名字(服务器上要存在的)
  */
 public boolean download(String savePath, String fileName,String downloadPath) {
try {
path=path+"/"+downloadPath;
connectServer(server, user, password, path);
} catch (Exception e) {
e.printStackTrace();
logger.debug("服务器开启失败!");
return false;
}
TelnetInputStream ftpIn = null;
FileOutputStream ftpOut = null;
try {
ftpIn = ftpClient.get(fileName); // fileName为FTP服务器上要下载的文件名
byte[] buf = new byte[204800];
int bufsize = 0;
ftpOut = new FileOutputStream(savePath + "\\" + fileName); // 存放在本地硬盘的物理位置
while ((bufsize = ftpIn.read(buf, 0, buf.length)) != -1) {
ftpOut.write(buf, 0, bufsize);


}
logger.debug("文件下载成功!");
} catch (Exception e) {
logger.debug("下载文件失败!");
e.printStackTrace();
return false;
} finally {
if (ftpIn != null) {
try {
ftpIn.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
if (ftpOut != null) {
try {
ftpOut.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
if (ftpClient != null) {
try {
ftpClient.closeServer();

} catch (IOException e) {
logger.debug("关闭服务器失败!");
e.printStackTrace();
return false;
}
}
}
return true;
}
注意:connectServer(server, user, password, path);中第四个参数如果是"/",是FTP用户对应的文件夹。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周凡首

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值