SSH ftp upload and download

Upload and download via Sftp

package com.bdsoft.ftp;

 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Vector;

import com.bdsoft.service.TimeTaskFtp;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

/**
 * WRITER--:ZCJ
 * EXPLAIN-: This method is used for FTP server connection, file upload, and file download public static methods that can be called directly by the class name
 */
public class MessageSFtpTools {
	private static  ChannelSftp sftp;
	private static   Channel channel;
	private static   Session session;
	public static void downloadFile(String other_flag,String other_content,String my_flag,String my_content,String hostname,int port,String username,String password) throws JSchException, SftpException{
		GetSftp(hostname, username, password, port);
		Vector list = sftp.ls(other_flag); //Get the file
        if(list.size()>0)
        {
        	 Iterator it = list.iterator();
             while (it.hasNext())
             {
               LsEntry entry = (LsEntry) it.next();
               String filename = entry.getFilename();
               / / Determine the file at the end of xml
               if(filename.endsWith("xml"))
               {
            	   TimeTaskFtp.logger.info("download "+filename+" to 接口机");
            	   sftp.get(other_flag+filename,my_flag); //download file
            	   sftp.rm(other_flag+filename);  //删除
               }
             }
        }
        sftp.disconnect();
        channel.disconnect();
        session.disconnect();
	}
	public static void uploadFile(String my_up_flag,String my_up_content,String other_flag,String other_up_content,String hostname,int port ,String username,String password) throws SocketException, IOException, JSchException, SftpException {
		 GetSftp(hostname, username, password, port);
		 File rootDir = new File(my_up_flag);
		 String[] fileList =  rootDir.list();
		 if(!rootDir.isDirectory()){
			 TimeTaskFtp.logger.info("Folder does not exist"+rootDir.getAbsolutePath());
		 }else{
			 for (int i = 0; i < fileList.length; i++) {
				    String filename = rootDir.getAbsolutePath()+"/"+fileList[i];
				    TimeTaskFtp.logger.info("up "+filename+" to SMS server");
				    File tempFile =new File(filename);
				    sftp.put(filename,other_flag);  //上传
					tempFile.delete();
				}
		 }
		  sftp.disconnect();
		channel.disconnect();
     	   session.disconnect();
	}
	/**
	* Method description: Get connection
	* @return
	*/
	public static void GetSftp(String hostname,String username,String password,int port) throws JSchException
	{
		String ftpHost = hostname;
        String ftpUserName = username;
        String ftpPassword = password;
        JSch jsch = new JSch(); // create a JSch object
        session = jsch.getSession(ftpUserName, ftpHost,port); // Get a Session object according to username, host ip, port
        if (ftpPassword != null) {
            session.setPassword(ftpPassword); // set password
        }
        session.setConfig("StrictHostKeyChecking", "no");
        session.setTimeout(300000); // Set the timeout time
        session.connect(); // establish connection through Session
        channel= session.openChannel("sftp"); // Open SFTP channel
        channel.connect(); // establish a connection to the SFTP channel
        sftp = (ChannelSftp) channel;
	}
	 
}   



Guess you like

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