Manipulate files on Linux server through sftp (java)

This article is to realize the operation of linux server files. Windows server does not support.

Introduce jar package: jsch-0.1.42.jar
package com.csvreader.sftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Properties;
import java.util.Vector;

import org.junit.Test;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

public class SftpUtil {
	/**
	 *
	 * @param host
	 * @param port
	 * @param username
	 * @param password
	 * @return
	 * @throws JSchException
	 */
	public ChannelSftp connect(String host, int port, String username,String password) throws JSchException {
		// 1. Declare the channel to connect to Sftp
		ChannelSftp nChannelSftp = null;
		// 2. Instantiate JSch
		JSch nJSch = new JSch();
		// 3. Get session
		Session nSShSession = nJSch.getSession(username, host, port);
		System.out.println("Session created successfully");
		// 4. Set the password
		nSShSession.setPassword(password);
		// 5. Instantiate Properties
		Properties nSSHConfig = new Properties();
		// 6. Set configuration information
		nSSHConfig.put("StrictHostKeyChecking", "no");
		// 7. Set configuration information in session
		nSShSession.setConfig(nSSHConfig);
		// 8.session connection
		nSShSession.connect();
		System.out.println("Session is connected");
		// 9. Open the sftp channel
		Channel channel = nSShSession.openChannel("sftp");
		// 10. Start connecting
		channel.connect();
		nChannelSftp = (ChannelSftp) channel;
		System.out.println("Connect to host" + host + ".");
		return nChannelSftp;
	}

	/**
	 * file renaming
	 * @param directory
	 * @param oldname
	 * @param newname
	 * @param sftp
	 */
	public void renameFile(String directory, String oldname, String newname,ChannelSftp sftp) {
		try {
			sftp.cd(directory);
			sftp.rename(oldname, newname);
		} catch (Exception e) {
			e.printStackTrace ();
		}
	}

	/**
	 * upload files
	 * @param directory
	 * @param uploadFile
	 * @param sftp
	 */
	public void upload(String directory, String uploadFile, ChannelSftp sftp) {
		try {
			sftp.cd(directory);
			File file = new File(uploadFile);
			sftp.put(new FileInputStream(file), file.getName());
		} catch (Exception e) {
			e.printStackTrace ();
		}
	}

	/**
	 * download file
	 * @param directory
	 * @param downloadFile
	 * @param saveFile
	 * @param sftp
	 */
	public void download(String directory, String downloadFile,String saveFile, ChannelSftp sftp) {
		try {
			sftp.cd(directory);
			File file = new File(saveFile);
			sftp.get(downloadFile, new FileOutputStream(file));

		} catch (Exception e) {
			e.printStackTrace ();
		}
	}

	/**
	 * Delete Files
	 * @param directory
	 * @param deleteFile
	 * @param sftp
	 */
	public void delete(String directory, String deleteFile, ChannelSftp sftp) {
		try {
			sftp.cd(directory);
			sftp.rm(deleteFile);
			System.out.println("Delete successful");
		} catch (Exception e) {
			System.out.println("Deletion failed");
			e.printStackTrace ();
		}
	}

	/**
	 * List the files under the list
	 * @param directory
	 * @param sftp
	 * @return
	 * @throws SftpException
	 */
	public Vector listFiles(String directory, ChannelSftp sftp)throws SftpException {
		return sftp.ls(directory);
	}

	/**
	 * Download all files under the folder
	 *
	 * @param viDirectory folder
	 * @param viHost hostname
	 * @param viPort port number
	 * @param viUserName username
	 * @param viPassWord user password
	 * @param viSaveDir save path
	 * @return
	 */
	@Test
	public List<String> downloadDirFile(String viDirectory, String viHost,int viPort, String viUserName, String viPassWord, String viSaveDir) {
		ChannelSftp nChannelSftp = null;
		List<String> nFileNameList = null;
		try {
			// 1. Instantiate the nSftpUtil tool class
			SftpUtil nSftpUtil = new SftpUtil();
			// 2. Establish Sftp channel
			nChannelSftp = nSftpUtil.connect(viHost, 22, viUserName, viPassWord);
			// 3. Get all the files under the directory
			Vector nVector = nChannelSftp.ls(viDirectory);
			// 4. Loop through the file
			for (int i = 0; i < nVector.size(); i++) {
				// 5. Enter the server folder
				nChannelSftp.cd(viDirectory);
				// 6. Instantiate the file object
				String nFileName = nVector.get(i).toString().substring(56, nVector.get(i).toString().length());
				if (!nFileName.contains("csv")) {
					continue;
				}
				File nFile = new File(viSaveDir + File.separator + nFileName);
				// 7. Download the file
				nChannelSftp.get(nFileName, new FileOutputStream(nFile));
			}
		} catch (Exception e) {
			e.printStackTrace ();
		} finally {
			nChannelSftp.disconnect();
		}
		return nFileNameList;
	}


Attached jar package

Guess you like

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