📄 ftptool.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package DownloadManager;import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.StringTokenizer;//import sun.net.TelnetInputStream;import sun.net.TelnetOutputStream;import sun.net.ftp.FtpClient;/** this class upload source codes to server @author Liu Yuyang */public class FtpTool { private FtpClient ftpclient; private String ipAddress; private int ipPort; private String userName; private String PassWord; /** Constructor @param ip String ftp server' ip @param port int provided port @param username String FTP username @param password String FTP password @throws java.lang.Exception */ public FtpTool(String ip, int port, String username, String password) throws Exception { ipAddress = new String(ip); ipPort = port; ftpclient = new FtpClient(ipAddress, ipPort); userName = new String(username); PassWord = new String(password); } /** Constructor @param ip String ftp server' ip @param username int provided port @param password String FTP username @throws java.lang.Exception */ public FtpTool(String ip, String username, String password) throws Exception { ipAddress = new String(ip); ipPort = 90; ftpclient = new FtpClient(ipAddress, ipPort); userName = new String(username); PassWord = new String(password); } /** Constructor @param username int provided port @param password String FTP username @throws java.lang.Exception */ public FtpTool(String username, String password) throws Exception { ipAddress = "168.131.152.215"; ipPort = 90; ftpclient = new FtpClient(ipAddress, ipPort); userName = new String(username); PassWord = new String(password); } /** * login FTP server * * @throws Exception */ public void login() throws Exception { ftpclient.login(userName, PassWord); } /** * logout FTP server * * @throws Exception */ public void logout() throws Exception { ftpclient.sendServer("QUIT\r\n "); //reply is used to test the reply number from ftp server int reply = ftpclient.readServerResponse(); } /** * This method is called when creating a specialized directory on the * ftp server while the directory is not existed. * * @param pathList ftp path which intend to upload e.g. "/upload/user1/test" here directory "/upload/user1" exist on the ftp server directory "/test" will be created. * @throws Exception */ public void createDirectory(String pathList) throws Exception { ftpclient.ascii(); StringTokenizer s = new StringTokenizer(pathList, "/"); int count = s.countTokens(); String pathName = ""; while (s.hasMoreElements()) { pathName = pathName + "/" + (String) s.nextElement(); try { ftpclient.sendServer("XMKD " + pathName + "\r\n "); int reply = ftpclient.readServerResponse(); } catch (Exception e) { e = null; } } ftpclient.binary(); } /** * 取得指定目录下的所有文件名,不包括目录名称 分析nameList得到的输入流中的数,得到指定目录下的所有文件名 * * @param fullPath * String * @return ArrayList * @throws Exception */ public ArrayList fileNames(String fullPath) throws Exception { ftpclient.ascii(); // 注意,使用字符模式 // try{ //TelnetInputStream list = ftpclient.nameList(fullPath+"/"); TelnetInputStream list; //list=ftpclient.get(fullPath); list = ftpclient.list(); byte[] names = new byte[2048]; int bufsize = 0; bufsize = list.read(names, 0, names.length); // 从流中读取 list.close(); ArrayList namesList = new ArrayList(); int i = 0; int j = 0; while (i < bufsize /* names.length */) { // char bc = (char) names; // System.out.println(i + " " + bc + " : " + (int) names); // i = i + 1; if (names[i] == 10) { // 字符模式为10,二进制模式为13 // 文件名在数据中开始下标为j,i-j为文件名的长度,文件名在数据中的结束下标为i-1 // System.out.write(names, j, i - j); // System.out.println(j + " " + i + " " + (i - j)); String tempName = new String(names, j, i - j); namesList.add(tempName); // System.out.println(temp); // 处理代码处 // j = i + 2; //上一次位置二进制模式 j = i + 1; // 上一次位置字符模式 } i = i + 1; } return namesList;// }catch(Exception e)// {// System.out.println(e);// // return null;// } } /** This method is called when upload a file to FTP server. There is no existed file check, when upload file is existed only override. @param source file's local path @param destination upload path on the server, it must include the file name. e.g. "/upload/user1/downFile.xml" @throws java.lang.Exception */ public void upFile(String source, String destination) throws Exception { createDirectory(destination.substring(0, destination.lastIndexOf("/"))); ftpclient.binary(); TelnetOutputStream ftpOut = ftpclient.put(destination); TelnetInputStream ftpIn = new TelnetInputStream(new FileInputStream(source), true); byte[] buf = new byte[1024]; int bufsize = 0; while ((bufsize = ftpIn.read(buf, 0, buf.length)) != -1) { ftpOut.write(buf, 0, bufsize); } ftpIn.close(); ftpOut.close(); } /** * 从FTP文件服务器上下载文件SourceFileName,到本地destinationFileName 所有的文件名中都要求包括完整的路径名在内 * * @param SourceFileName * String * @param destinationFileName * String * @throws Exception */ public void downFile(String SourceFileName, String destinationFileName) throws Exception { ftpclient.binary(); // 一定要使用二进制模式 TelnetInputStream ftpIn = ftpclient.get(SourceFileName); byte[] buf = new byte[204800]; int bufsize = 0; FileOutputStream ftpOut = new FileOutputStream(destinationFileName); while ((bufsize = ftpIn.read(buf, 0, buf.length)) != -1) { ftpOut.write(buf, 0, bufsize); } ftpOut.close(); ftpIn.close(); } /** * 从FTP文件服务器上下载文件,输出到字节数组中 * * @param SourceFileName * String * @return byte[] * @throws Exception */ public byte[] downFile(String SourceFileName) throws Exception { ftpclient.binary(); // 一定要使用二进制模式 TelnetInputStream ftpIn = ftpclient.get(SourceFileName); ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); byte[] buf = new byte[204800]; int bufsize = 0; while ((bufsize = ftpIn.read(buf, 0, buf.length)) != -1) { byteOut.write(buf, 0, bufsize); } byte[] return_arraybyte = byteOut.toByteArray(); byteOut.close(); ftpIn.close(); return return_arraybyte; } public void delFilesFromFTP(String file) { ftpclient.sendServer("DELE " + file + "\r\n "); } /** This method is for test @param args @throws java.lang.Exception */ public static void main(String[] args) throws Exception { String ip = "168.131.152.215"; int port = 90; String userName = "Admin"; String password = "123456"; String destinationFile = "d:/"; FtpTool fUp = new FtpTool(ip, port, userName, password); fUp.login(); String remoteRoot = "/upload/user1/test/"; //fUp.upFile("d:/downFile.xml", "/upload/user1/test/"); //fUp.downFile("/upload/user1/file.txt", "d:/file.txt"); //get file list under one folder List fileList = fUp.fileNames("/upload"); for (int i = 3; i < fileList.size(); i++) { String fileName = fileList.get(i).toString(); System.out.println(fileName); String tempFile = destinationFile + fileName.substring(fileName.lastIndexOf("/ ")); fUp.downFile(fileName, tempFile); } fUp.logout(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -