SFTP上传文件

package com.iekun.ef.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

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

public class SftpFactory {

	public static void main(String[] args) {
	}
	
	private SftpFactory(){}
	
	public static void sshSftp(String ip, String user, String psw, int port, String sPath, String dPath) {
		Session session = null;
		JSch jsch = new JSch();
		try {
			if (port <= 0) {
				session = jsch.getSession(user, ip);
			} else {
				session = jsch.getSession(user, ip, port);
			}
			if (session == null) {
				throw new Exception("session is null");
			}
			session.setPassword(psw);
			session.setConfig("StrictHostKeyChecking", "no");
			session.connect(3000);
			UpLoadFile.upLoadFile(session, sPath, dPath);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("uploading success!");
	}
}

class UpLoadFile {
	public static void upLoadFile(Session session, String sPath, String dPath) {
		Channel channel = null;
		try {
			channel = (Channel) session.openChannel("sftp");
			channel.connect(10000000);
			ChannelSftp sftp = (ChannelSftp) channel;
			try {
				sftp.cd(dPath);
			} catch (SftpException e) {
				sftp.mkdir(dPath);
				sftp.cd(dPath);
			}
			File file = new File(sPath);
			copyFile(sftp, file, sftp.pwd());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.disconnect();
			channel.disconnect();
		}
	}

	public static void copyFile(ChannelSftp sftp, File file, String pwd) {
		if (file.isDirectory()) {
			File[] list = file.listFiles();
			try {
				try {
					String fileName = file.getName();
					sftp.cd(pwd);
					sftp.mkdir(fileName);
					System.out.println("create directory:" + sftp.pwd() + "/" + fileName);
				} catch (Exception e) {
				}
				pwd = pwd + "/" + file.getName();
				try {
					sftp.cd(file.getName());
				} catch (SftpException e) {
					e.printStackTrace();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			for (int i = 0; i < list.length; i++) {
				copyFile(sftp, list[i], pwd);
			}
		} else {
			try {
				sftp.cd(pwd);
			} catch (SftpException e1) {
				e1.printStackTrace();
			}
			System.out.println("copy file :" + file.getAbsolutePath());
			InputStream instream = null;
			OutputStream outstream = null;
			try {
				outstream = sftp.put(file.getName());
				instream = new FileInputStream(file);
				byte b[] = new byte[1024];
				int n;
				try {
					while ((n = instream.read(b)) != -1) {
						outstream.write(b, 0, n);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}

			} catch (SftpException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					outstream.flush();
					outstream.close();
					instream.close();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
		}
	}
}

 

posted @ 2017-09-25 16:33  Djokovic  阅读(491)  评论(0编辑  收藏  举报