java使用commons-net-2.2访问ftp
2014-05-13 16:23
357 查看
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; /** * ftp操作工具类 * @author dell * */ public class FtpClientUtil { /** * 打开ftp服务器连接 * * @param ftpClient * @param server * ftp的url * @param port * 端口 * @param userName * 登陆用户名 * @param password * 登陆密码 * @return */ public static boolean open(FTPClient ftpClient, String server, int port, String userName, String password) { if (ftpClient == null) { ftpClient = new FTPClient(); } // 已经打开连接则直接返回 if (ftpClient.isConnected()) return true; try { // ftpClient.setControlEncoding("GBK"); // 连接服务器 ftpClient.connect(server, port); ftpClient.login(userName, password); if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { ftpClient.disconnect(); return false; } ftpClient.setControlEncoding("GBK"); return true; } catch (Exception e) { e.printStackTrace(); ftpClient = null; return false; } } /** * 打开ftp上某个目录 * * @param ftpClient * @param dir * @return */ public static boolean cd(FTPClient ftpClient, String dir) { boolean f = false; try { ftpClient.changeWorkingDirectory(dir); } catch (IOException e) { return f; } return true; } /** * 取得指定路径下的满足要求的文件名列表 * * @param ftpClient * @param server * @param port * @param userName * @param password * @param dir * ftp路径 * @param begin * 文件名过滤,以什么字符串开始,不需要此过滤传入null * @param end * 文件名过滤,以什么字符串结束,不需要此过滤传入null * @return 出错返回null,正常为文件名list */ public static List<String> files(FTPClient ftpClient, String dir, String begin, String end) { try { // 取得文件名流 cd(ftpClient, dir); FTPFile[] names = ftpClient.listFiles(); List<String> files = new ArrayList<String>(); // 读取文件名 for (FTPFile name : names) { // 文件名过滤 if (begin != null && !name.getName().startsWith(begin)) { continue; } if (end != null && !name.getName().endsWith(end)) { continue; } files.add(name.getName()); } return files; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 上传文件到FTP服务器 * * @param is * 本地文件流 * @param ftpFileName * 上传后的文件名 * @param ftpDirectory * FTP目录如:/path1/pathb2/,如果目录不存在回自动创建目录 * @throws Exception */ public static boolean upload(FTPClient ftpClient, InputStream is, String ftpFileName, String ftpDirectory) throws Exception { try { ftpClient.makeDirectory(ftpDirectory); cd(ftpClient, ftpDirectory); ftpClient.storeFile(ftpFileName, is); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 从FTP服务器上下载文件并返回文件流 * * @param ftpDirAndFileName * @param ftpClient * @return * @throws Exception */ public static InputStream download2File(FTPClient ftpClient, String ftpDirAndFileName) throws Exception { try { ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); return ftpClient.retrieveFileStream(charEncode(ftpDirAndFileName)); } catch (Exception e) { return null; } } /** * 从FTP服务器上下载文件并返回文件流 * * @param ftpDirAndFileName * @param localDirAndFileName * @param ftpClient * @return * @throws Exception */ public static OutputStream download2File(FTPClient ftpClient, String ftpDirAndFileName, File f) throws Exception { try { OutputStream os = new FileOutputStream(f); ftpClient.setBufferSize(1024); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpClient.retrieveFile(charEncode(ftpDirAndFileName), os); return os; } catch (Exception e) { return null; } } /** * 删除FTP上的文件 * * @param ftpDirAndFileName */ public static boolean deleteFile(FTPClient ftpClient, String ftpDirAndFileName) throws Exception { try { ftpClient.deleteFile(charEncode(ftpDirAndFileName)); return true; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } /** * 关闭链接 */ public static void close(FTPClient ftpClient) { try { if (ftpClient != null && ftpClient.isConnected()) ftpClient.logout(); ftpClient.disconnect(); } catch (Exception e) { } } /** * 转码 * @param str * @return * @throws Exception */ public static String charEncode(String str) throws Exception { return new String((str).getBytes("GBK"), "iso-8859-1"); } }
相关文章推荐
- java ftp 客户端(apache jakarta commons net ftp)的bug[原创][Bug]
- 使用org.apache.commons.net.ftp包开发FTP客户端,实现进度汇报,实现断点续传,中文支持
- 使用Commons-net包实现ftp服务器的访问(转)
- 基于 apache 的 commons-net-3.3.jar 的 ftp java代码上传下载文件
- 基于apache的commons-net-3.3.jar的 ftp java代码上传下载文件
- org.apache.commons.net.ftp.FTPClient使用总结
- sun.net.ftp.FtpClient(java访问/操作ftp)
- linux 上使用commons-net.jar中 FTPClient类的listFiles、retrieveFile等方法停止不执行,出现假死状态解决办法
- 使用org.apache.commons.net.ftp包开发FTP客户端,实现进度汇报,实现断点续传,中文支持
- 使用org.apache.commons.net.ftp包开发FTP客户端,实现进度汇报,实现断点续传,中文支持
- linux下使用commons-net ftp的总结
- 使用org.apache.commons.net.ftp包开发FTP客户端,实现进度汇报,实现断点续传,中文支持
- java通过commons-net上传文件到ftp服务器
- 使用org.apache.commons.net.ftp包开发FTP客户端,实现进度汇报,实现断点续传,中文支持
- 关于Java.net.URL对象使用Proxy访问Internet资源
- [Android中级]使用Commons-net-ftp来实现FTP上传、下载的功能
- java ftp org.apache.commons.net.io.CopyStreamException
- 关于Java.net.URL对象使用Proxy访问Internet资源
- java.lang.NoClassDefFoundError: org.apache.commons.net.ftp.FTPClient
- 关于Java.net.URL对象使用Proxy访问Internet资源