1 package org.migle.util;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.SocketException;
9
10 import org.apachemons.logging.Log;
11 import org.apachemons.logging.LogFactory;
12
13 import org.apachemons.ftp.FTP;
14 import org.apachemons.ftp.FTPClient;
15 import org.apachemons.ftp.FTPReply;
16
17 /**
18 * FTP Client工具类,封装了<a href="/net/">Jakarta Commons Net
19 * FTPClient</a>对常用的功能如上传、下载 等功能提供简易操作,如果需要Jakarta Commons Net 20 * FTPClient的全部功能可以通过{@link #getFtpclient()}
21 * 得到org.apachemons.ftp.FTPClient
22 * 对象,对于org.apachemons.ftp.FTPClient的全部功能有请查看 <a href =
23 * "/net/api/org/apache/commons/net/ftp/FTPClient.html"
24 泰山亚龙湾水上乐园 * > FTPClient API </a>
25 *
26 * @since V2.0
27 * @version V1.0 2010-2-26
28 * @author 研发中心
29 */
30 public class FTPClientUtil {
31
32 private static Log logger = Log(FTPClient.class);
33
34 private FTPClient ftpclient;
35
36 public FTPClientUtil(String host, int port) throws Exception {
37 this(host, port, null, null);
38 }
39
40 public FTPClientUtil(String host, int port, String username, String password)
41 throws Exception { 证券投资基金运作管理办法
42 ftpclient = new FTPClient();
43 try {
44 t(host, port);
45 int reply = ReplyCode();
46 if (!FTPReply.isPositiveCompletion(reply)) {
47 ftpclient.disconnect();
48 logger.fatal("FTP服务器拒绝连接"); 49 throw new Exception("FTP服务器拒绝连接");
50 }
51 if (username != null) {
52 if (!ftpclient.login(username, password)) {
53 ftpclient.disconnect();
54 logger.fatal("登陆验证失败,请检查账号和密码是否正确");
55 throw new Exception("登陆验证失败,请检查账号和密码是否正确");
56 }
57 }
58 } catch (SocketException e) {
59 logger.fatal("无法连接至指定FTP服务器", e); 60 throw new Exception(e);
61 } catch (IOException e) {
62 logger.fatal("无法用指定用户名和密码连接至指定FTP服务器", e);
63 throw new Exception(e);
64 }
65 }
66
67 /**
68 *
69 * @param path
70 * 文件在FTP上存储的绝路径
71 * @param input
72 * 上传文
73 * @throws IOException
74 */
75 public boolean upload(String pathname, InputStream input)
76 throws IOException {
77 // 是否是在根目录 下
78 ftpclient.setFileType(FTP.BINARY_FILE_TYPE);
79 if (pathname.indexOf("/") != -1) {
80 String path = pathname.substring(0, pathname.lastIndexOf("/"));
81 mkdir(path);
82 } 密特朗总统
整数规划83 return ftpclient.storeFile(new Bytes(), ControlEncoding()), input);
84 }
85
86 /**
87 * 从FTP服务器上下载pathname指定的文件,命名为localName
88 *
89 * @param pathname
90 * @param localName
91 * @return
92 * @throws Exception
93 */
94 public boolean download(String pathname, String localName) throws Exception {
95 String filename = localName != null ? localName : pathname
96 .substring(pathname.lastIndexOf("/") + 1);
97
98 if (filename == null || filename.isEmpty()) {
99 斩波器 return false;
100 }
101
102 // 设置被动模式
103 LocalPassiveMode();
104 // 设置以二进制方式传输
105 ftpclient.setFileType(FTP.BINARY_FILE_TYPE);
106
107 if (ftpclient.listFiles(new Bytes(),ControlEncoding())).length == 0) {
108 logger.fatal("下载文件不存在");
109 throw new Exception("下载文件不存在");
110 }
111
112 File tmp = new File(filename + "_tmp"); // 临时文件
113 File file = new File(filename);
114 FileOutputStream output = null;
115 郧阳师专中文系 boolean flag;
116 try {
117 output = new FileOutputStream(tmp);
118 flag = ieveFile(new Bytes(),ControlEncoding()), output);
119 output.close();
120 if (flag) {
121 // 下载成功,重命名临时文件。
122 ameTo(file);