ftp 实现文件的上传下载以及列出文件列表Java代码
来源:互联网 发布:php setslaveokay 编辑:程序博客网 时间:2024/05/17 03:43
public class FtpUtil {private Log log = LogFactory.getLog(getClass()) ;private String userName; private String password;private String ip; private int port; private FTPClient ftpClient = null; private FTPSClient ftps = null ;//构造方法初始化类public FtpUtil(String userName, String password, String ip, int port) {this.userName = userName;this.password = password;this.ip = ip;this.port = port;} //连接ftppublic boolean connectServer() throws Exception{boolean flag = true;if (ftpClient == null) { ftpClient = new FTPClient();ftpClient.connect(ip,port);log.info("Connected to " + ip);log.info(ftpClient.getReplyString());int reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) {ftpClient.disconnect();log.warn("FTP server refused connection.");return false ;}boolean bok = ftpClient.login(userName, password);if (!bok) {try {ftpClient.disconnect() ;ftpClient = null ;} catch (Exception e) { }throw new Exception("can not login ftp server") ;}ftpClient.setBufferSize(1024);ftpClient.setControlEncoding("GBK"); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.setDataTimeout(120000); ftpClient.enterLocalPassiveMode();ftpClient.setUseEPSVwithIPv4(false);} return flag;} //列出所有文件内容public List<String> listRemoteAllFiles(String path) throws Exception {ftpClient.enterLocalPassiveMode(); FTPFile[] files = ftpClient.listFiles(path, new FTPFileFilter() { @Overridepublic boolean accept(FTPFile file) { if (file.isFile()) return true ;return false ;}}) ;List<String> list = new ArrayList() ;for (FTPFile file : files) {list.add(file.getName()) ;}return list ;}public void closeConnect() { try {if (ftpClient != null) {ftpClient.logout();ftpClient.disconnect();}} catch (Exception e) {}} //下载文件public boolean downloadFile(String remotePath, String fileName, String localPath) throws Exception {FileOutputStream fos = null ; try {File localFile = new File(localPath, fileName);fos = new FileOutputStream(localFile);ftpClient.enterLocalPassiveMode(); ftpClient.changeWorkingDirectory(remotePath) ;boolean bok = ftpClient.retrieveFile(fileName, fos);fos.close() ;fos = null ;return bok ;} catch (Exception e) {throw e ;}finally {if (fos!=null) {try {fos.close() ;fos = null ;} catch (Exception e2) { }}} } //上传文件public boolean uploadFile(String remotePath, String filename, String localFilePath) throws Exception {FileInputStream fis = null ;try {fis = new FileInputStream(new File(localFilePath));ftpClient.enterLocalPassiveMode(); ftpClient.changeWorkingDirectory(remotePath);boolean bok = ftpClient.storeFile(filename, fis); fis.close();fis = null ;return bok ;} catch (Exception e) {throw e ;}finally {if (fis!=null) {try {fis.close() ;fis = null ;} catch (Exception e2) { }}}}//删除文件public boolean removeFile(String remotePath, String filename) throws Exception {ftpClient.changeWorkingDirectory(remotePath);boolean bok = ftpClient.deleteFile(filename) ; return bok ;}}
0 0
- ftp 实现文件的上传下载以及列出文件列表Java代码
- ftp 实现文件的上传下载以及列出文件列表Java代码(未测试)
- Java中实现FTP上传下载文件的功能,完整代码
- Java中实现FTP上传下载文件的功能,完整代码
- java实现ftp文件上传下载
- Java实现FTP上传下载文件
- Java 实现ftp上传下载文件
- java+jsp代码实现从FTP服务器上传下载文件
- 实现 ftp 文件/文件夹的上传下载以及删除
- JAVA实现FTP文件上传下载文件
- python列出ftp文件列表
- Java FTP上传下载单个文件示例代码
- java ftp 文件上传下载
- java FTP上传下载文件
- java FTP上传下载文件
- Java FTP 实现跨服务器文件上传下载
- Java FTP 实现跨服务器文件上传下载
- Java FTP 实现跨服务器文件上传下载
- LeetCode199. Binary Tree Right Side View
- 关于原生js的一些研究
- LeetCode上Tag为广度优先搜索BFS(Breadth-first Search)的题目整理
- 【汇编】JMP跳转指令的指令长度、直接转移与间接转移、段内跳转与段间跳转
- LeetCode 3. Longest Substring Without Repeating Characters
- ftp 实现文件的上传下载以及列出文件列表Java代码
- EM算法(2)
- LeetCode上Tag为贪心算法(Greedy)的题目整理
- LeetCode 55. Jump Game
- Java 代码实现七牛云存储图片
- LeetCode 134. Gas Station
- java获取当前月第一天和最后一天,上个月第一天和最后一天
- LeetCode 330. Patching Array
- 预编译小常识