Ftp upload and download

*. There is such a demand in actual project development. In order to perform confidential operations, build an FTP server on an external server, and then perform active data capture operations in ftp mode on the internal confidential server, and so on~

 

----Welcome to Paizhuan, and you are welcome to give us your advice---

(~ ̄▽ ̄)~

 

*, login (parameters are simply encapsulated)

public static FTPClient getFtpConn(JBConfig jbConfig) {
	if(!ftpClient.isConnected()) {
		String remoteIP = "";
		String userName = "";
		String userPwd = "";
		try {
			remoteIP = jbConfig.getRemoteIP();
			userName = jbConfig.getUserName();
			userPwd = jbConfig.getUserPwd();
			ftpClient.connect(remoteIP);
			boolean f = ftpClient.login(userName , userPwd);
			if(f) {
				logger.warn("IP:"+remoteIP+", username: "+userName+" database connection is successful!");
			}
		}catch(IOException e) {
			logger.warn("IP:"+remoteIP+", username: "+userName+" database connection failed!"+e);
		}
	}
	return ftpClient;
}

 

*, download (internal tool functions can be written at will, O(∩_∩)O haha~)

/**
 * FTP download single file test
 * @param remoteFileName the absolute path of the remote environment
 * @param aimDir The root directory of the locally downloaded file
 */
public static String ftpDownFile(JBConfig jbConfig , String remoteFileName , String aimDir) {
	getFtpConn(jbConfig);
	FileOutputStream fos = null;
	String fileName = "";//The file name of the remote file
	String localFileName = "";//The location of the file to be downloaded locally
	if(!FileTool.isExist(aimDir)) {
		System.exit(0);//Exit if the directory file creation fails!
	}
	try {
		fileName = FileTool.getFileName(remoteFileName);//Get the file name of the remote file
		if(aimDir.contains("/")) {
			localFileName = aimDir+"/"+fileName;
		}else if(aimDir.contains("\\")) {
			localFileName = aimDir+"\\"+fileName;
		}
		logger.warn("File name extraction result is: "+fileName);
		logger.warn("Download to the local directory address path is: "+localFileName);
		
		fos = new FileOutputStream(localFileName);
		
		ftpClient.setBufferSize(1024);
		ftpClient.setControlEncoding("UTF-8");
		//Set the file type (binary)
		ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
		logger.warn(remoteFileName+":Downloading!!!");
		ftpClient.enterLocalPassiveMode();//Key point
		boolean b = ftpClient.retrieveFile(new String(remoteFileName.getBytes("UTF-8"),"ISO-8859-1"), fos);//Solve the problem of Chinese path
		if(b) {
			logger.warn(remoteFileName+":Download successful!!!");
		}else {
			logger.warn(remoteFileName+": Download failed!!!");
			return null;
		}
	} catch (IOException e) {
		logger.warn("FTP client error while downloading!"+e);
	} finally {
		IOUtils.closeQuietly (fos);
		try {
			ftpClient.disconnect();
		} catch (IOException e) {
			logger.warn("An exception occurred when closing the FTP connection during download!"+e);
		}
	}
	return localFileName;
}

 

*, upload

/**
 * FTP upload a single file
 */
public static boolean ftpUpFile(JBConfig jbConfig , String localFileName , String aimfileName) {
	getFtpConn(jbConfig);
	FileInputStream fis = null;
	try {
		//local file path
		File srcFile = new File(localFileName);
		fis = new FileInputStream(srcFile);
		//Set upload directory
		ftpClient.setBufferSize(1024);
		ftpClient.setControlEncoding("UTF-8");
		//Set the file type (binary)
		ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
		logger.warn(localFileName+":Uploading!!!");
		ftpClient.enterLocalPassiveMode();
		boolean b = ftpClient.storeFile(aimfileName, fis);
		if(b) {
			logger.warn(localFileName+":Upload successful!!!");
		}else {
			logger.warn(localFileName+":Upload failed!!!");
		}
	} catch (IOException e) {
		logger.warn("FTP client error while uploading!"+e);
		return false;
	} finally {
		IOUtils.closeQuietly (fis);
		try {
			ftpClient.disconnect();
		} catch (IOException e) {
			logger.warn("An exception occurred when closing the FTP connection during uploading!"+e);
		}
	}
	return true;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326037885&siteId=291194637