|
001 | import java.io.DataInputStream; |
003 | import java.io.FileInputStream; |
004 | import java.io.FileOutputStream; |
005 | import java.io.IOException; |
006 | import java.util.ArrayList; |
007 | import java.util.List; |
009 | import org.apache.catalina.tribes.util.Logs; |
011 | import sun.net.TelnetInputStream; |
012 | import sun.net.TelnetOutputStream; |
013 | import sun.net.ftp.FtpClient; |
015 | public class DownFileForFtp { |
017 | private String server = "ip"; |
018 | private int port = 21; |
019 | private String userName = "usn"; |
020 | private String userPassword = "pwd"; |
027 | public boolean open() { |
028 | if (ftpClient != null && ftpClient.serverIsOpen()) |
031 | ftpClient = new FtpClient(); |
032 | ftpClient.openServer(server, port); |
033 | ftpClient.login(userName, userPassword); |
036 | } catch (Exception e) { |
043 | public boolean cd(String dir) { |
047 | } catch (IOException e) { |
048 | //Logs.error(e.toString()); |
057 | * @param localPathAndFileName |
061 | * @param ftpDirectory |
062 | * FTP目录如:/path1/pathb2/,如果目录不存在回自动创建目录 |
065 | public boolean upload(String localDirectoryAndFileName, String ftpFileName, |
066 | String ftpDirectory) throws Exception { |
069 | FileInputStream is = null; |
070 | TelnetOutputStream os = null; |
073 | if (ftpDirectory.length() > 0) |
074 | ch = ftpDirectory.charAt(ftpDirectory.length() - 1); |
075 | for (; ch == '/' || ch == '\\'; ch = ftpDirectory |
076 | .charAt(ftpDirectory.length() - 1)) |
077 | ftpDirectory = ftpDirectory.substring(0, |
078 | ftpDirectory.length() - 1); |
080 | int slashIndex = ftpDirectory.indexOf(47); |
081 | int backslashIndex = ftpDirectory.indexOf(92); |
082 | int index = slashIndex; |
083 | String dirall = ftpDirectory; |
084 | if (backslashIndex != -1 && (index == -1 || index > backslashIndex)) |
085 | index = backslashIndex; |
086 | String directory = ""; |
087 | while (index != -1) { |
089 | String dir = dirall.substring(0, index); |
090 | directory = directory + "/" + dir; |
091 | ftpClient.sendServer("XMKD " + directory + "\r\n"); |
092 | ftpClient.readServerResponse(); |
094 | dirall = dirall.substring(index + 1); |
095 | slashIndex = dirall.indexOf(47); |
096 | backslashIndex = dirall.indexOf(92); |
098 | if (backslashIndex != -1 |
099 | && (index == -1 || index > backslashIndex)) |
100 | index = backslashIndex; |
102 | ftpClient.sendServer("XMKD " + ftpDirectory + "\r\n"); |
103 | ftpClient.readServerResponse(); |
105 | os = ftpClient.put(ftpDirectory + "/" + ftpFileName); |
106 | File file_in = new File(localDirectoryAndFileName); |
107 | is = new FileInputStream(file_in); |
108 | byte bytes[] = new byte[1024]; |
110 | while ((i = is.read(bytes)) != -1) |
111 | os.write(bytes, 0, i); |
115 | } catch (Exception e) { |
127 | * 从FTP服务器上下载文件并返回下载文件长度 |
129 | * @param ftpDirectoryAndFileName |
130 | * @param localDirectoryAndFileName |
134 | public long download(String ftpDirectoryAndFileName, |
135 | String localDirectoryAndFileName) throws Exception { |
139 | TelnetInputStream is = null; |
140 | FileOutputStream os = null; |
142 | is = ftpClient.get(ftpDirectoryAndFileName); |
143 | java.io.File outfile = new java.io.File(localDirectoryAndFileName); |
144 | os = new FileOutputStream(outfile); |
145 | byte[] bytes = new byte[1024]; |
147 | while ((c = is.read(bytes)) != -1) { |
148 | os.write(bytes, 0, c); |
151 | } catch (Exception e) { |
166 | * @param ftpDirectory |
169 | public List<String> getFileNameList(String ftpDirectory) { |
170 | List<String> list = new ArrayList<String>(); |
174 | DataInputStream dis = new DataInputStream( |
175 | ftpClient.nameList(ftpDirectory)); |
176 | String filename = ""; |
177 | while ((filename = dis.readLine()) != null) { |
179 | System.out.println(filename); |
181 | } catch (Exception e) { |
190 | * @param ftpDirAndFileName |
192 | public boolean deleteFile(String ftpDirAndFileName) { |
195 | ftpClient.sendServer("DELE " + ftpDirAndFileName + "\r\n"); |
202 | * @param ftpDirectory |
204 | public boolean deleteDirectory(String ftpDirectory) { |
207 | ftpClient.sendServer("XRMD " + ftpDirectory + "\r\n"); |
214 | public void close() { |
216 | if (ftpClient != null && ftpClient.serverIsOpen()) |
217 | ftpClient.closeServer(); |
218 | } catch (Exception e) { |
223 | public static void main(String []args) throws Exception{ |
224 | DownFileForFtp df = new DownFileForFtp(); |
225 | df.getFileNameList("E:\\uploadfiles\\cat"); |
226 | df.download("E:\\uploadfiles\\cat\\2012-03-29.11.00.00.012.xml", "F:\\temp\\ftpdown\\2012-03-29.11.00.00.012.xml"); |
|
|
|