Recently to get a little knowledge, create links sftp
rely
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.50</version>
</dependency>
code show as below
package com.example.demo.test; import com.jcraft.jsch.*; import org.springframework.util.StringUtils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Properties; public class SftpTest { public static void main(String[] args) { String putLocalPath = "E:/Report50.docx"; String getLocalPath = "E:/"; String name = "Report50.docx"; String sftpPath = "/ss/dd/ffs"; files(putLocalPath,sftpPath,name,"123456","root","192.168.1.1",22); } public static void files(String localPath,String sftpPath,String name,String pwd,String user,String ip,Integer port) { Session root=null; Channel channel = null; ChannelSftp channelSftp=null; try { // Get the session JSch jSch = new JSch(); // Set the user name, server ip, port number root = jSch.getSession(user, ip, port); //set password root.setPassword(pwd); // set after the first landing, free landing close Properties sshConfig = new Properties(); sshConfig.setProperty("StrictHostKeyChecking","no"); root.setConfig(sshConfig); root.connect(); channel = root.openChannel("sftp"); channel.connect(); channelSftp = (ChannelSftp)channel; mkdir(channelSftp,sftpPath); channelSftp.cd(sftpPath); // putFile(channelSftp,new File(localPath),sftpPath); // getFile(channelSftp,sftpPath,localPath,name); deleteSftp(channelSftp,sftpPath,sftpPath); } catch ( JSchException |SftpException e) { e.printStackTrace (); }finally { close(channelSftp,channel,root); } } /** * Upload the files in the path specified server * @param channelSftp * @param f * @param dst * @return */ public static String putFile(ChannelSftp channelSftp,File f, String dst){ try { channelSftp.cd(dst); channelSftp.put(new FileInputStream(f),f.getName()); } catch (FileNotFoundException | SftpException e) { e.printStackTrace (); return "Upload failed"; } return "Upload successful"; } /** * Download * @param channelSftp * @Param sftpPath * @param localPath * @param name * @return */ public static String getFile(ChannelSftp channelSftp,String sftpPath,String localPath,String name){ try { channelSftp.cd(sftpPath); channelSftp.get(name,localPath); } catch (SftpException e) { e.printStackTrace (); return "Download failed"; } return "download success"; } /** * Delete * @param channelSftp * @Param sftpPath * @param name * @return */ public static String deleteSftp(ChannelSftp channelSftp,String sftpPath,String name){ try { channelSftp.cd(sftpPath); channelSftp.rm(name); } catch (SftpException e) { e.printStackTrace (); return "Delete failed"; } return "deleted successfully"; } /** Determining whether there is a path * * @param channelSftp * @Param dirPath path * @return */ public static SftpATTRS isDirExist (ChannelSftp channelSftp,String dirPath){ SftpATTRS attrs = null; try { System.out.println(channelSftp.pwd()); // attrs = channelSftp.stat(dirPath); attrs = channelSftp.lstat(dirPath); } catch (Exception e) { // e.printStackTrace (); } return attrs; } public static void mkdir(ChannelSftp channelSftp,String dirPath){ SftpATTRS attrs = isDirExist(channelSftp,dirPath); if(attrs == null){ try { channelSftp.cd("/"); String[] dirs = dirPath.split("/"); for(int i=0;i<dirs.length;i++){ if(StringUtils.isEmpty(dirs[i])){ continue; } attrs = isDirExist(channelSftp,dirs[i]); if(attrs == null){ channelSftp.mkdir(dirs[i]); channelSftp.cd(dirs[i]); }else { channelSftp.cd(dirs[i]); } } } catch (SftpException e) { e.printStackTrace (); System.out.println ( "creation failed"); } } } public static void close(ChannelSftp channelSftp,Channel channel,Session session) { if(channel != null){ channel.disconnect(); } if(channelSftp != null){ channelSftp.disconnect(); } if(session != null){ session.disconnect(); } } }