Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package th.in.oneauthen;
- import java.io.File;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import com.jcraft.jsch.Channel;
- import com.jcraft.jsch.ChannelSftp;
- import com.jcraft.jsch.JSch;
- import com.jcraft.jsch.JSchException;
- import com.jcraft.jsch.Session;
- import com.jcraft.jsch.SftpException;
- public class uploadOneFile {
- public static void main(String[] args) {
- System.out.println(System.getProperty("os.name"));
- String hostname = "161.246.35.229";
- String username = "medserv";
- String password = "MeddyCall2018";
- String copyTo = "/home/testsftp/temp";
- String copyFrom = "C:\\Users\\BallZaR5R5\\Desktop\\fail\\";
- JSch jsch = new JSch();
- Session session = null;
- System.out.println("Trying to connect.....");
- try {
- session = jsch.getSession(username, hostname, 22);
- session.setConfig("StrictHostKeyChecking", "no");
- session.setPassword(password);
- session.connect();
- Channel channel = session.openChannel("sftp");
- channel.connect();
- File folder = new File(copyFrom);
- File[] listOfFiles = folder.listFiles();
- ChannelSftp sftpChannel = (ChannelSftp) channel;
- for (File file : listOfFiles) {
- if (file.isFile()) {
- System.out.println(file.getName());
- sftpChannel.put(copyFrom + file.getName() , copyTo);
- String Del = copyFrom + file.getName();
- // Files.delete(Paths.get(Del));
- }
- }
- // ChannelSftp sftpChannel = (ChannelSftp) channel;
- // sftpChannel.put(copyFrom, copyTo);
- sftpChannel.exit();
- session.disconnect();
- } catch (JSchException e) {
- e.printStackTrace();
- } catch (SftpException e) {
- e.printStackTrace();
- }
- System.out.println("Done !!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement