浏览 3001 次
锁定老帖子 主题:ftp上传下载
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-11-30
package com.ftp.example; import java.io.IOException; import java.text.ParseException; import java.util.ArrayList; import java.util.List; import com.enterprisedt.net.ftp.FTPException; import com.enterprisedt.net.ftp.FTPFile; import com.enterprisedt.net.ftp.FTPTransferType; import com.enterprisedt.net.ftp.FileTransferClient; public class FtpClientExample { /** * * @param ftpip ftpIP地址 * @param user 用户 * @param pswd 密码 * @return */ public FileTransferClient getConnect(String ftpip, String user, String pswd) { FileTransferClient client = new FileTransferClient(); try { client.setRemoteHost(ftpip); client.setUserName(user); client.setPassword(pswd); client.setRemotePort(Integer.parseInt("21")); client.connect(); if (client.isConnected()) { client.setContentType(FTPTransferType.BINARY); System.out.println("------ftp连接成功!!"); client.getAdvancedFTPSettings().setControlEncoding("GB2312"); return client; } else { System.out.println("------不能登录FTP服务器,请和管理员联系!!"); return null; } } catch (FTPException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 上传到ftp * @param localhostPath 本地的文件路径 * @param ftpPath 上传存放文件的ftp路径 * @param FileName 存放到ftp文件名 * @return * @throws FTPException * @throws IOException * @throws ParseException */ public boolean uploadFileToFtp(String localhostPath, String ftpPath, String FileName) throws FTPException, IOException, ParseException { String pathFile = localhostPath; String reFile = ftpPath + FileName; FileTransferClient client = this.getConnect("127.0.0.1", "pas", "pas"); int length = client.directoryList(ftpPath).length; if (length <= 0) { System.out.println("FTP文件路径错误"); return false; } client.uploadFile(pathFile, reFile); client.disconnect(); return true; } /** * 从ftp下载到本地 * @param ftpPath * @param fileNames * @param localhostPath * @return * @throws FTPException * @throws IOException * @throws ParseException */ public boolean downloadFileFromFtp(String ftpPath, List fileNames, String localhostPath) throws FTPException, IOException, ParseException { String pathFile = localhostPath; String reFile = ftpPath; FileTransferClient client = this.getConnect("127.0.0.1", "pas", "pas"); int length = client.directoryList(ftpPath).length; if (length <= 0) { System.out.println("ftp路径" + ftpPath + "不是正确:"); return false; } System.out.println("可下载的文件个数" + fileNames.size()); for (int i = 0; i < fileNames.size(); i++) { String fileName = (String) fileNames.get(i); pathFile = pathFile + "\\" + fileName; reFile = reFile + fileName; System.out.println(reFile + "是否存在:" + client.exists(reFile)); if (client.exists(reFile)) { System.out.println("pathFile--" + pathFile); System.out.println("reFile--" + reFile); client.downloadFile(pathFile, reFile); client.deleteFile(reFile); pathFile = localhostPath; reFile = ftpPath; continue; } } client.disconnect(); return true; } /** * 获取文件夹下面所有的子目录文件 * * @param filePath * @return * @throws ParseException * @throws IOException * @throws FTPException */ public List getPathFile(String filePath) throws FTPException, IOException, ParseException { List list = new ArrayList(); FileTransferClient client = this.getConnect("127.0.0.1", "pas", "pas"); FTPFile diretoryNames[]; diretoryNames = client.directoryList(filePath); if (diretoryNames.length > 0) // 目录下面有文件 { for (int j = 0; j < diretoryNames.length; j++) { if (diretoryNames[j].getName().equals(".") || diretoryNames[j].getName().equals("..")) continue; list.add(diretoryNames[j].getName()); } } client.disconnect(); return list; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub FtpClientExample example = new FtpClientExample(); try { List list = example.getPathFile("/bank/010/sqzf/"); System.out.println(list.size()); } catch (FTPException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2010-11-30
注释太少了
|
|
返回顶楼 | |
发表时间:2011-03-08
买的企业版?
|
|
返回顶楼 | |