1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
| import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.RandomAccessFile; import org.apache.log4j.Logger; import sun.net.TelnetOutputStream; import sun.net.TelnetInputStream; import sun.net.ftp.FtpClient;
public class FTPclient { private String localfilename; private String localSource;
private String remotefilename;
private static final Logger log = Logger.getLogger( FTPclient.class.getName() ); FtpClient ftpClient;
public void connectServer(String ip, int port,String user , String password,String path) {
try { ftpClient = new FtpClient(); ftpClient.openServer(ip,port); ftpClient.login(user, password); log.info("ftpPaht="+ip+":" + "port:" + port +"login success!"); if (path.length() != 0) ftpClient.cd(path); ftpClient.binary(); } catch (IOException ex) { log.info("ftpPaht="+ip+":" + "port:" + port +"not login"); log.info(ex); } }
public void closeConnect() { try { ftpClient.closeServer(); log.info("disconnect success"); } catch (IOException ex) { log.info("not disconnect"); log.info(ex); } }
public void upload() {
try { TelnetOutputStream os = ftpClient.put(this.remotefilename); java.io.File file_in = new java.io.File(this.localfilename); FileInputStream is = new FileInputStream(file_in); byte[] bytes = new byte[1024]; int c; while ((c = is.read(bytes)) != -1) { os.write(bytes, 0, c); } log.info(localfilename + "upload success"); is.close(); os.close(); } catch (IOException ex) { log.info(localfilename + "not upload"); log.info(ex); } } public void download() {
try { TelnetInputStream is = ftpClient.get(this.remotefilename); java.io.File file_in = new java.io.File(this.localfilename); FileOutputStream os = new FileOutputStream(file_in); byte[] bytes = new byte[1024]; int c; while ((c = is.read(bytes)) != -1) { os.write(bytes, 0, c); }
log.info(remotefilename + "download success"); os.close(); is.close(); } catch (IOException ex) { log.info(remotefilename + "not download"); log.info(ex); } }
public void download(String remotePath,String remoteFile,String localFile) {
try { if (remotePath.length() != 0) ftpClient.cd(remotePath); TelnetInputStream is = ftpClient.get(remoteFile); java.io.File file_in = new java.io.File(localFile); FileOutputStream os = new FileOutputStream(file_in); byte[] bytes = new byte[1024]; int c; while ((c = is.read(bytes)) != -1) { os.write(bytes, 0, c); }
log.info(remoteFile + "download success"); os.close(); is.close(); } catch (IOException ex) { log.info(remoteFile + "not download"); log.info(ex); } }
public void download(String remoteFile,String localFile) {
try { TelnetInputStream is = ftpClient.get(remoteFile); java.io.File file_in = new java.io.File(localFile); FileOutputStream os = new FileOutputStream(file_in); byte[] bytes = new byte[1024]; int c; while ((c = is.read(bytes)) != -1) { os.write(bytes, 0, c); }
log.info(remoteFile + "download success"); os.close(); is.close(); } catch (IOException ex) { log.info(remoteFile + "not download"); log.info(ex); } }
public void makeDirectory(String ftpdirectory) throws IOException{
try { char ch = ' '; if (ftpdirectory.length() > 0) ch = ftpdirectory.charAt(ftpdirectory.length() - 1); for (; ch == '/' || ch == '\\'; ch = ftpdirectory.charAt(ftpdirectory.length() - 1)) ftpdirectory = ftpdirectory.substring(0, ftpdirectory.length() - 1);
int slashindex = ftpdirectory.indexOf(47); int backslashindex = ftpdirectory.indexOf(92); int index = slashindex; String dirall = ftpdirectory; if (backslashindex != -1 && (index == -1 || index > backslashindex)) index = backslashindex; String directory = ""; while (index != -1) { if (index > 0) { String dir = dirall.substring(0, index); directory = directory + "/" + dir; ftpClient.sendServer("xmkd " + directory + "\r\n"); ftpClient.readServerResponse(); } dirall = dirall.substring(index + 1); slashindex = dirall.indexOf(47); backslashindex = dirall.indexOf(92); index = slashindex; if (backslashindex != -1 && (index == -1 || index > backslashindex)) index = backslashindex; } ftpClient.sendServer("xmkd " + ftpdirectory + "\r\n"); ftpClient.readServerResponse(); } catch(Exception e) { e.printStackTrace();
} } public static void main(String agrs[]) {
String filepath[] = { "/callcenter/index.jsp", "/callcenter/ip.txt", "/callcenter/mainframe/image/processing_bar_2.gif", "/callcenter/mainframe/image/logo_01.jpg" }; String localfilepath[] = { "C:\\FTP_Test\\index.jsp", "C:\\FTP_Test\\ip.txt", "C:\\FTP_Test\\processing_bar_2.gif", "C:\\FTP_Test\\logo_01.jpg" };
FTPclient fu = new FTPclient(); fu.connectServer("172.16.1.66",22, "web_test", "123456","/callcenter"); for(int i=0;i<filepath.length;i++){ fu.download(filepath[i],localfilepath[i]); }
fu.closeConnect();
}
public String getLocalfilename() { return localfilename; }
public void setLocalfilename(String localfilename) { this.localfilename = localfilename; }
public String getRemotefilename() { return remotefilename; }
public void setRemotefilename(String remotefilename) { this.remotefilename = remotefilename; }
public String getLocalSource() { return localSource; }
public void setLocalSource(String localSource) { this.localSource = localSource; } }
|