Advertisement
ballchaichana

uploadoneFile

Aug 27th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 KB | None | 0 0
  1. package th.in.oneauthen;
  2.  
  3. import java.io.File;
  4. import java.nio.file.Files;
  5. import java.nio.file.Paths;
  6.  
  7. import com.jcraft.jsch.Channel;
  8. import com.jcraft.jsch.ChannelSftp;
  9. import com.jcraft.jsch.JSch;
  10. import com.jcraft.jsch.JSchException;
  11. import com.jcraft.jsch.Session;
  12. import com.jcraft.jsch.SftpException;
  13.  
  14. public class uploadOneFile {
  15.  
  16.     public static void main(String[] args) {
  17.         System.out.println(System.getProperty("os.name"));
  18.         String hostname = "161.246.35.229";
  19.         String username = "medserv";
  20.         String password = "MeddyCall2018";
  21.         String copyTo = "/home/testsftp/temp";
  22.         String copyFrom = "C:\\Users\\BallZaR5R5\\Desktop\\fail\\";
  23.         JSch jsch = new JSch();
  24.         Session session = null;
  25.         System.out.println("Trying to connect.....");
  26.         try {
  27.  
  28.             session = jsch.getSession(username, hostname, 22);
  29.             session.setConfig("StrictHostKeyChecking", "no");
  30.             session.setPassword(password);
  31.             session.connect();
  32.             Channel channel = session.openChannel("sftp");
  33.             channel.connect();
  34.  
  35.             File folder = new File(copyFrom);
  36.             File[] listOfFiles = folder.listFiles();
  37.             ChannelSftp sftpChannel = (ChannelSftp) channel;
  38.  
  39.             for (File file : listOfFiles) {
  40.                 if (file.isFile()) {
  41.                     System.out.println(file.getName());
  42.                     sftpChannel.put(copyFrom + file.getName()  , copyTo);
  43.                     String Del = copyFrom + file.getName();
  44.                 //  Files.delete(Paths.get(Del));
  45.                 }
  46.             }
  47.  
  48. //          ChannelSftp sftpChannel = (ChannelSftp) channel;
  49. //          sftpChannel.put(copyFrom, copyTo);
  50.             sftpChannel.exit();
  51.             session.disconnect();
  52.         } catch (JSchException e) {
  53.             e.printStackTrace();
  54.         } catch (SftpException e) {
  55.             e.printStackTrace();
  56.         }
  57.         System.out.println("Done !!");
  58.  
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement