1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
| package cn.likfees.common.utils.file;
import cn.likfees.common.config.DockerConfig; import com.jcraft.jsch.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;
import java.io.*; import java.util.Properties; import java.util.Vector;
@Component public class SftpUtils {
@Autowired private DockerConfig dockerConfig;
private Session session = null;
public void connect() {
JSch jSch = new JSch(); try { session = jSch.getSession(dockerConfig.getUsername(), dockerConfig.getDockerHost()); session.setPassword(dockerConfig.getPassword()); Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.setTimeout(30000); session.connect(); } catch (JSchException e) { throw new RuntimeException(String.format("SFTP getSession() 失败! host: %s; username: %s, password: %s, err_msg: %s", dockerConfig.getDockerHost(), dockerConfig.getUsername(), dockerConfig.getPassword(), e.getMessage())); }
}
public void execCmd(String command) throws JSchException { BufferedReader reader = null;
Channel channel = null; try { if (command != null) { channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand(command); channel.connect();
InputStream in = channel.getInputStream(); reader = new BufferedReader(new InputStreamReader(in)); String buf = null; while ((buf = reader.readLine()) != null) { System.out.println(buf); } } } catch (IOException | JSchException e) { e.printStackTrace(); } finally { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } if (channel != null) { channel.disconnect(); } } }
public void upload(String uploadFile, String directory) throws JSchException, SftpException, FileNotFoundException { ChannelSftp channelSftp = null; try { channelSftp = (ChannelSftp) session.openChannel("sftp"); channelSftp.connect(); channelSftp.cd(directory); File file = new File(uploadFile); channelSftp.put(new FileInputStream(file), file.getName()); } finally { assert channelSftp != null; channelSftp.disconnect(); }
}
public void download(String src, String dst) throws Exception { ChannelSftp channelSftp = null; try { channelSftp = (ChannelSftp) session.openChannel("sftp"); channelSftp.connect(); channelSftp.get(src, dst); } finally { assert channelSftp != null; channelSftp.disconnect(); } }
public Vector listFiles(String path) throws Exception { ChannelSftp channelSftp = null; try { channelSftp = (ChannelSftp) session.openChannel("sftp"); channelSftp.connect(); return channelSftp.ls(path); } finally { assert channelSftp != null; channelSftp.disconnect(); } }
public void delete(String directory, String deleteFile) throws Exception { ChannelSftp channelSftp = null; try { channelSftp = (ChannelSftp) session.openChannel("sftp"); channelSftp.connect(); channelSftp.cd(directory); channelSftp.rm(deleteFile); } finally { assert channelSftp != null; channelSftp.disconnect(); } }
public void close() { if (session != null) { session.disconnect(); } } }
|