SoFunction
Updated on 2025-03-03

How to download remote server files using ftp

Java uses ftp to download remote server files

The first method

Download it remotely with the account password in the connection:

    public Result<?> download(){
		//Download the file ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------        //Remote server download address        String fileurl = "ftp://superAdmin:[email protected]:8888/usr/mp4pt/rec/CHN0/10110120011100/00001.mp4";
        //File physical path        String realfilepath = "F:/rhsftdemo/00001.mp4";
        // Create a new folder directory in the local area        File file = new File("F:/rhsftdemo/");
        if (!()) {
            ();
        }
        ("Storage path realfilepath:" + realfilepath + "Download path:" + fileurl);
        download(fileurl,realfilepath);
        return ();
    }

    private static final int BUFFER_SIZE = 4096;
    public void download(String ftpUrl,String savePath){
        long startTime = ();
//        String ftpUrl = "ftp:///misc/unixutil/";
        String file = "";
        // name of the file which has to be download
        String host = ""; // ftp server
        String user = ""; // user name of the ftp server
        String pass = ""; // password of the ftp server
//        String savePath = "F:\\";
        ftpUrl = (ftpUrl, user, pass, host);
        ("Connecting to FTP server");
        try {
            URL url = new URL(ftpUrl);
            URLConnection conn = ();
            InputStream inputStream = ();
            long filesize = ();
            ("Size of the file to download in kb is:-" + filesize / 1024);
            FileOutputStream outputStream = new FileOutputStream(savePath);
            byte[] buffer = new byte[BUFFER_SIZE];
            int bytesRead = -1;
            while ((bytesRead = (buffer)) != -1) {
                (buffer, 0, bytesRead);
            }
            long endTime = ();
            ("File downloaded");
            ("Download time in sec. is:-" + (endTime - startTime) / 1000);
            ();
            ();
        } catch (IOException ex) {
            ();
        }
    }

The second method

Log in with your account password and download it:

   public Result<?> download(){
        //Download the file ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------        //Remote server download address        String fileurl = "ftp://192.168.1.8:8888/usr/mp4pt/rec/CHN0/20220420095100/00001.mp4";
        // Local file physical path        String realfilepath = "F:/rhsftdemo/00001.mp4";
        // Create a new folder directory in the local area        File file = new File("F:/rhsftdemo/");
        if (!()) {
            ();
        }
        ("Storage path realfilepath:" + realfilepath + "Download path:" + fileurl);
        boolean bb = retepasvfile(realfilepath,fileurl,"00001.mp4");
        return ();
    }


/**
      * Local save
      *
      * @param localurl physical path to local file
      * @param hosturl Remote server download address
      * @param filename filename
      *
      * @return
      */
    public synchronized boolean retepasvfile(String localurl, String hosturl, String filename) {
        FTPClient ftp = new FTPClient();
        boolean re = false;
        try {
            ("Local storage path==:" + localurl);
            File file = (localurl);
            (10000);
            (10000);
            ("192.168.1.8", 21);
            if (!("admin") && !("superAdmin123") && "superAdmin" != "null" && "superAdmin123" != "null") {
                //Enter account password here                ("superAdmin", "superAdmin123");
            } else {
                ("Start link ftp:");
                ("anonymous", "anonymous");
            }

            //();
            //  String str=();
            (); //Passive mode            (60);
            (FTP.BINARY_FILE_TYPE);
            boolean b = ((0, ("/")));
            ("pwd:" + b + "---Switch directory:" + (0, ("/")));
            long locationsize = ();//Size of file on the server            if (b) {
                boolean res = false;
                res = (filename, new FileOutputStream(file));
                if (res) {
                    //();
                    ("File download completed");
                    re = true;
                } else {
                    ("File download failed");
                    //();
                }
            }
        } catch (Exception e) {
            ();

            ("retepasvfileSamba:e=3" + e);
        } finally {
            try {
                ();
            } catch (Exception e) {
            }
        }
        return re;
    }

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.