File upload and download in Java (based on FTP protocol)

Role of FTP Server

FTP servers (File Transfer Protocol Servers) are computers that provide file storage and access services over the Internet. They provide services according to the FTP protocol. FTP servers are often used for file sharing and transmission and are an essential part of the Internet.

Our file upload and download function is the practical application of file sharing and transfer function!

(3) How FTP works:

Like many Internet applications, FTP services are a client-side reverse server system (C/S). The user connects to the FTP server program on the remote host through a client program that supports the FTP protocol. The user sends commands to the server program through the client program, which executes the commands issued by the user and returns the results to the client.

Take file download as an example. When you start an FTP client program to download files from a remote computer, two programs are actually started: an FTP client program on your computer that makes a request to the FTP server to copy the downloaded files;
 

Set up FTP server

Import required JAR files

 

Upload to FTP Server

1. Create FTP client object;

       FTPClient ftpclient= new FTPClient();

2. Create a file input stream for creating local files;

        FileInputStream fis = new FileInputStream(tempExcelFile);

3. Connect to FTP server;

        ftpclient.connect("192.168.254.197",21);

4. Login;

        boolean isLogin = ftpclient.login("*********", "******************");

5. Switch the specified directory;

        ftpclient.changeWorkingDirectory("htpDownload");

6. Set file type;

        ftpclient.setFileType(FTPClient.BINARY_FILE_TYPE);
7. Upload local files to FTP server.

        ftpclient.storeFile(tempExcelFile.getName(),fis);

public boolean upLoadToFTP(Vector<String> colNameVector, Vector<Vector<Object>> dataVector) {
		//Generate a temporary excel
		File tempExcelFile = new File("D:\\tupian11\\temp\\"+System.currentTimeMillis()+".xlsx");
		//Write data to temporary excel
		exportExcel(colNameVector, dataVector, tempExcelFile);
		//upload
		FTPClient ftpclient= new FTPClient();
		try (FileInputStream fis = new FileInputStream(tempExcelFile);){
			ftpclient.connect("192.168.254.197",21);
			boolean isLogin = ftpclient.login("root", "root");
			System.out.println("Login Successful"+isLogin);
			//Change directory
			ftpclient.changeWorkingDirectory("htpDownload");
			//Upload Storage File
			ftpclient.setFileType(FTPClient.BINARY_FILE_TYPE);
			ftpclient.storeFile(tempExcelFile.getName(),fis);
		} catch (SocketException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}finally {
			try {
				ftpclient.disconnect();
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
		}
		
		//Delete temporary Excel
		tempExcelFile.delete();
		
		return true;
		}

        

 

Download files from FTP server

1. Create FTP client object;

 FTPClient ftpclient= new FTPClient();

2. Create a file output stream to download file writes from the FTP server;

FileOutputStream fos = new FileOutputStream("D:\\tupian11\\DownLoadAuto\\"+fileName)

3. Connect to FTP server;

 ftpclient.connect("192.168.254.197",21);

4. Login;

  boolean isLogin = ftpclient.login("*********", "******************");

5. Switch the specified directory;

  ftpclient.changeWorkingDirectory("htpDownload");

6. Set file type;

  ftpclient.setFileType(FTPClient.BINARY_FILE_TYPE);

7. Download the specified file from the FTP server.

    boolean isretrieve =ftpclient.retrieveFile(outFileName, fos);
            return isretrieve;

The sample code is as follows:

public boolean downLoad(String fileName,String outFileName) {
		//Determine if the incoming path exists
		File downLoadDir = new File("D:\\tupian11\\DownLoadAuto");
		if(!downLoadDir.exists()) {
			downLoadDir.mkdir();
		}
		FTPClient ftpclient = new FTPClient();
		try (FileOutputStream fos = new FileOutputStream("D:\\tupian11\\DownLoadAuto\\"+fileName)){
			//ftp
			ftpclient.connect("192.168.254.197",21);
			boolean isLogin = ftpclient.login("root", "root");
			System.out.println("Login Successful"+isLogin);
			ftpclient.changeWorkingDirectory("htpDownload");
			
			ftpclient.setFileType(FTPClient.BINARY_FILE_TYPE);
			boolean isretrieve =ftpclient.retrieveFile(outFileName, fos);
			return isretrieve;
		} catch (SocketException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}finally {
			try {
				ftpclient.disconnect();
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
		}

	}

Tags: Linux network server

Posted by Liquix on Mon, 19 Sep 2022 21:02:52 +0300