Recommend a tool class for java to operate ftp

Posted by tmallen on Wed, 04 Dec 2019 11:53:15 +0100

Written in front

As children's shoes that often use computers to organize files, they should have used to upload and download files from the ftp server. Today, we will learn how to operate files of ftp service through java programs

First of all, you should know the ip, path, port, account and password with operation permission of ftp

1 import jar package

 commons-net-3.6.jar

This jar package is used to set the encoding. After testing, it can be used without adding

2 main methods in tool class

2.1 login ftp

	/**
	 * Verify login
	 * @param ip
	 * @param port
	 * @param name
	 * @param pwd
	 * @return
	 */
	public boolean login(String ip,int port, String name, String pwd) {
		try {
			ftp = new FTPClient();
			ftp.connect(ip, port);
			System.out.println(ftp.login(name, pwd));
			if(!ftp.login(name, pwd)){
				return false;
			}
			ftp.setCharset(Charset.forName("UTF-8"));
			ftp.setControlEncoding("UTF-8");

		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

Note: to obtain the remote file directory, the upload and download methods are based on the login method

2.2 get remote file directory

	/**
	 * Get the file name under a file (path) of ftp to view the file list
	 * @param ip
	 * @param port
	 * @param name
	 * @param pwd
	 * @param remotedir Remote address directory
	 * @return
	 */
    public boolean getFilesName(String ip,int port, String name, String pwd, String remotedir) {
        try {
        	if(!login(ip, port, name, pwd)){
				return false;
			}
            //Get ftp, specify the file name in the folder, and store it in the array
            FTPFile[] files = ftp.listFiles(remotedir);
            //Print out the file name in the ftp folder
            for (int i = 0; i < files.length; i++) {
                System.out.println(files[i].getName());
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally{
        	this.close();
        }
        return true;
    }

2.3 upload files

	/**
     * Upload file method 1
     * @param ip
     * @param port
     * @param name
     * @param pwd
     * @param remotepath Remote address file path
     * @param localpath Local file path
     * @return
     */
    public boolean putFileOne(String ip,int port, String name, String pwd,String remotepath,String localpath) {
        try {
        	if(!login(ip, port, name, pwd)){
				return false;
			}
            //Upload the local local path file to the root folder of ftp and rename it to the name in remotepath
        	 return ftp.storeFile(remotepath, new FileInputStream(new File(localpath)));
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally{
        	this.close();
        }
    }
    
    /**
     * The second method of uploading files optimizes the transmission speed
     * @param ip
     * @param port
     * @param name
     * @param pwd
     * @param remotepath Remote address file path
     * @param localpath Local file path
     * @return
     */
    public boolean putFileTwo(String ip,int port, String name, String pwd,String remotepath,String localpath) {
        try {
        	if(!login(ip, port, name, pwd)){
				return false;
			}
            os = ftp.storeFileStream(remotepath);
            fis = new FileInputStream(new File(localpath));
            byte[] b = new byte[1024];
            int len = 0;
            while ((len = fis.read(b)) != -1) {
                os.write(b,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally {
        	this.close();
		}
        return true;
    }

2.4 downloading files

	/**
     * Download file method 1
     * @param ip
     * @param port
     * @param name
     * @param pwd
     * @param remotepath Remote address file path
     * @param localpath Local file path
     * @return
     */
    public boolean getFileOne(String ip,int port, String name, String pwd,String remotepath,String localpath) {
        try {
        	if(!login(ip, port, name, pwd)){
				return false;
			}
            //Download the remotepath file in the ftp resource to the local directory folder and rename it to the name in localpath
        	return ftp.retrieveFile(remotepath, new FileOutputStream(new File(localpath)));
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally{
        	this.close();
        }
    }
	
    /**
     * The second method of downloading files optimizes the transmission speed
     * @param ip
     * @param port
     * @param name
     * @param pwd
     * @param remotepath Remote address file path
     * @param localpath  Local file path
     * @return
     */
	public boolean getFileTwo(String ip,int port, String name, String pwd,String remotepath,String localpath) {
		try {
			if(!login(ip, port, name, pwd)){
				return false;
			}
			is = ftp.retrieveFileStream(remotepath);
			fos = new FileOutputStream(new File(localpath));
			byte[] b = new byte[1024];
			int len = 0;
			while ((len = is.read(b)) != -1) {
				fos.write(b,0,len);
			}
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}finally {
			this.close();
		}
		return true;
	}

3 source code

Of course, the above code is only an important part. If you have any problems, you can go to github to download it by yourself charmsongo

If there is any better way, please leave a message

Topics: Programming ftp Java encoding github