Java语言操作FTP服务进行文件的处理
2014-08-20 17:57
399 查看
1、接口:FtpClient.java
2、实现类:FtpClientEDTImpl.java
3、异常处理类:
4、下载文件使用方法:
5、测试:
import com.enterprisedt.net.ftp.FTPFile; import java.io.File; import java.io.OutputStream; import java.util.List; import java.util.Map; /** * * 类<strong>FtpClient.java</strong>FTP服务接口 * * @author: wangyf * @version: 1.0 Date: 2011-9-1上午10:41:48 */ public abstract interface FtpClient { public abstract void initFfpClient(String paramString, int paramInt) throws FtpException; public abstract void initFfpClientUTF8(String paramString, int paramInt) throws FtpException; public abstract void login(String paramString1, String paramString2) throws FtpException; public abstract void cd(String paramString) throws FtpException; public abstract void mkDir(String paramString) throws FtpException; public abstract FTPFile[] list(String paramString) throws FtpException; public abstract boolean uploadFiles(File[] paramArrayOfFile, String paramString) throws FtpException; public abstract boolean uploadFiles(List<File> paramList, String paramString) throws FtpException; public abstract boolean uploadFiles(Map<String, byte[]> paramMap, String paramString) throws FtpException; public abstract boolean uploadFiles2(Map<String, String> paramMap, String paramString) throws FtpException; public abstract void get(OutputStream paramOutputStream, String paramString) throws FtpException; public abstract byte[] get(String paramString) throws FtpException; public abstract void get(String paramString1, String paramString2) throws FtpException; public abstract boolean quit() throws FtpException; public void del(String fileName) throws FtpException; }
2、实现类:FtpClientEDTImpl.java
import com.enterprisedt.net.ftp.FTPClient; import com.enterprisedt.net.ftp.FTPConnectMode; import com.enterprisedt.net.ftp.FTPException; import com.enterprisedt.net.ftp.FTPFile; import com.enterprisedt.net.ftp.FTPTransferType; import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.log4j.Logger; /** * * 类<strong>FtpClient.java</strong>FTP服务实现 * * @author: wangyf * @version: 1.0 Date: 2011-9-1上午10:51:48 */ public class FtpClientEDTImpl implements FtpClient { private static final Logger log = Logger.getLogger(FtpClientEDTImpl.class); private FTPClient client = null; public boolean quit() throws FtpException { if ((this.client != null) && (this.client.connected())) try { this.client.quit(); return true; } catch (Exception e) { throw new FtpException(e); } return false; } public void initFfpClient(String host, int port) throws FtpException { this.client = new FTPClient(); try { this.client.setRemoteHost(host); this.client.setRemotePort(port); this.client.setControlEncoding("GBK"); this.client.setConnectMode(FTPConnectMode.ACTIVE); this.client.connect(); } catch (Exception e) { throw new FtpException(e); } } public void initFfpClientUTF8(String host, int port) throws FtpException { this.client = new FTPClient(); try { this.client.setRemoteHost(host); this.client.setRemotePort(port); this.client.setControlEncoding("UTF-8"); this.client.setConnectMode(FTPConnectMode.ACTIVE); this.client.connect(); } catch (Exception e) { throw new FtpException(e); } } public void login(String user, String pwd) throws FtpException { if ((this.client != null) && (this.client.connected())) try { this.client.login(user, pwd); this.client.setConnectMode(FTPConnectMode.PASV); this.client.setType(FTPTransferType.BINARY); } catch (Exception e) { throw new FtpException(e); } } public boolean uploadFiles(File[] files, String destDir) throws FtpException { if ((this.client != null) && (this.client.connected())) { for (File file : files) try { this.client.put(file.getPath(), destDir + "/" + file.getName()); } catch (Exception e) { throw new FtpException(e); } return true; } return false; } public boolean uploadFiles(List<File> files, String destDir) throws FtpException { if ((this.client != null) && (this.client.connected())) { for (File file : files) try { this.client.put(file.getPath(), destDir + "/" + file.getName()); } catch (Exception e) { throw new FtpException(e); } return true; } return false; } public boolean uploadFiles(Map<String, byte[]> files, String destDir) throws FtpException { if ((this.client != null) && (this.client.connected())) { Set<String> keys = files.keySet(); for (String filename : keys) { byte[] bytes = (byte[])files.get(filename); try { this.client.put(bytes, destDir + "/" + filename, false); } catch (Exception e) { throw new FtpException(e); } } return true; } return false; } public boolean uploadFiles2(Map<String, String> files, String destDir) throws FtpException { if ((this.client != null) && (this.client.connected())) { Set<String> keys = files.keySet(); for (String filename : keys) { String filepath = (String)files.get(filename); try { this.client.put(filepath, destDir + "/" + filename, false); } catch (Exception e) { throw new FtpException(e); } } return true; } return false; } public void mkDir(String destDir) throws FtpException { try { if (this.client.exists(destDir)) return; this.client.mkdir(destDir); } catch (Exception e) { log.info("文件夹‘" + destDir + "’已存在!"); } } public void cd(String rPath) throws FtpException { try { this.client.chdir(rPath); } catch (IOException e) { e.printStackTrace(); } catch (FTPException e) { e.printStackTrace(); } } public void get(OutputStream os, String remoteFile) throws FtpException { try { this.client.get(os, remoteFile); } catch (Exception e) { e.printStackTrace(); throw new FtpException(e); } } public byte[] get(String remoteFile) throws FtpException { try { return this.client.get(remoteFile); } catch (Exception e) { e.printStackTrace(); throw new FtpException(e); } } public void get(String localPath, String remoteFile) throws FtpException { try { this.client.get(localPath, remoteFile); } catch (Exception e) { e.printStackTrace(); throw new FtpException(e); } } public FTPFile[] list(String path) throws FtpException { FTPFile[] files = new FTPFile[0]; try { files = this.client.dirDetails(path); } catch (Exception e) { e.printStackTrace(); throw new FtpException(e); } return files; } public void del(String fileName) throws FtpException { try { this.client.delete(fileName); } catch (Exception e) { e.printStackTrace(); throw new FtpException(e); } } }
3、异常处理类:
/** * * 类<strong>FtpClient.java</strong>FTP异常类 * * @author: wangyf * @version: 1.0 Date: 2011-9-1上午10:58:48 */ public class FtpException extends Exception { private static final long serialVersionUID = 1L; public FtpException(Exception e) { super(e); } }
4、下载文件使用方法:
/** * 通过FTP服务到银联WAP的对应FTP服务器上下载对账文件 * @param hostname--IP * @param port--端口 * @param username--用户名 * @param password--密码 * @param remotePath--银联FTP服务器对账文件路径 * @param remoteFilename--银联对账文件名 * @param localFilename--放在本地的对账文件名 * @return--是否下载成功 */ public boolean downloadFile(String hostname, int port, String username, String password, String remotePath, String remoteFilename, String localFilename) { byte[] bytes = null; FtpClient ftpClient = new FtpClientEDTImpl(); try { ftpClient.initFfpClientUTF8(hostname.trim(), port); ftpClient.login(username.trim(), password.trim()); bytes = ftpClient.get(remotePath + remoteFilename); bytes = ftpClient.get(remoteFilename); PaymentUtils.writeToFile(localFilename, bytes); log.info("通过ftp协议获取银联WAP对账文件成功!"); return true; } catch (FtpException e) { log.error("通过ftp协议获取银联WAP对账文件出错:", e); } catch (Exception e) { log.error("通过ftp协议获取银联WAP对账文件出错:", e); } finally { try { if (null != ftpClient) { ftpClient.quit(); } } catch (FtpException e) { log.error("ftp客户端退出时出错:", e); } } return false; }
5、测试:
public static void main(String[] args) { UnionPayWapChckActFileRetrieve actFileRetrieve = new UnionPayWapChckActFileRetrieve(); boolean isDownload; isDownload=actFileRetrieve.downloadFile("210.51.51.185", 21, "userName", "pswd", "", "630056832596_20140810.txt", "E:\\630056832596_20140810.txt"); if(isDownload==true){ System.out.println("下载对账文件成功............"); }else{ System.out.println("下载对账文件失败............"); } }
相关文章推荐
- java 操作FTP进行文件操作
- 从ApplicationResources.properties中读取数据对文件进行操作.java
- Java语言对properties资源文件的处理——ResourceBundle 类的使用
- java中IO操作 利用文件流 过滤流对文件进行读写操作
- Java 如何对文件进行多个Object对象流的读写操作
- java中使用JXL对Excel文件进行数据的写入、导出操作
- 在Java 7里如何对文件进行操作
- JAVA读取XML文件并利用该文件对数据库进行配置操作
- Java 如何对文件进行多个Object对象流的读写操作
- java中IO操作 利用文件流 过滤流对文件进行读写操作
- 在Java 7里如何对文件进行操作
- Java 文件操作及字符流处理
- Java进行FTP操作
- java中,对文件进行追加内容操作的三种方法
- 在Java 7里如何对文件进行操作
- Java语言对properties资源文件的处理 - ResourceBundle类的用
- Java通过Samba服务操作Linux共享文件
- java对文件进行加密解密操作
- Java创建TXT文件并进行读、写、修改操作
- Java进行FTP操作