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; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; import java.util.Vector; public class SftpClientUtil { /** * 密码方式登录 读取指定目录文件 * * @param ip * @param user * @param psw * @param port */ public static Session sshSftpReadFile(String ip, String user, String psw, int port) { 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);// 设置密码 // 设置第一次登陆的时候提示,可选值:(ask | yes | no) session.setConfig("StrictHostKeyChecking", "no"); // 设置登陆超时时间 session.connect(300000); } catch (Exception e) { e.printStackTrace(); } return session; } /** * 密码方式登录 上传指定目录文件 * * @param ip * @param user * @param psw * @param port */ public static File sshSftpUpLoadFile(String ip, String user, String psw, int port, String sPath,File file) { Session session = null; File folderToScan = 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);// 设置密码 // 设置第一次登陆的时候提示,可选值:(ask | yes | no) session.setConfig("StrictHostKeyChecking", "no"); // 设置登陆超时时间 session.connect(300000); upLoadFile(session,file,sPath); } catch (Exception e) { e.printStackTrace(); } return folderToScan; } public static Map<String, InputStream > readFile(Session session, String sPath) { Channel channel = null; Map<String, InputStream > stringHashMap =new HashMap<>(); try { channel = (Channel) session.openChannel("sftp"); channel.connect(10000000); ChannelSftp sftp = (ChannelSftp) channel; try { sftp.cd(sPath); } catch (SftpException e) { sftp.mkdir(sPath); sftp.cd(sPath); } Vector<ChannelSftp.LsEntry> listFiles = sftp.ls(sftp.pwd()); for (ChannelSftp.LsEntry file : listFiles) { String fileName = file.getFilename(); try { InputStream inputStream = sftp.get(sftp.pwd() + "/" + fileName); stringHashMap.put(fileName,inputStream); } catch (SftpException e) { e.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } return stringHashMap; } public static void upLoadFile(Session session, File file , String sPath) { Channel channel = null; try { channel = (Channel) session.openChannel("sftp"); channel.connect(10000000); ChannelSftp sftp = (ChannelSftp) channel; try { sftp.cd(sPath); // } catch (SftpException e) { sftp.mkdir(sPath); sftp.cd(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); } catch (Exception e) { // TODO: handle exception } pwd = pwd + "/" + file.getName(); try { sftp.cd(file.getName()); } catch (SftpException e) { // TODO: handle exception e.printStackTrace(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } for (int i = 0; i < list.length; i++) { copyFile(sftp, list[i], pwd); } } else { try { sftp.cd(pwd); } catch (SftpException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } 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 (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (SftpException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { outstream.flush(); outstream.close(); instream.close(); } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); } } } } }
java 通过SFTP连接,获取指定目录文件和上传文件
最新推荐文章于 2024-08-13 18:32:02 发布