Transfer file to linux server in java


To transfer the file through SSH protocol we have to download two jar files : jsch-0.1.42.jar, xercesImpl.jar
JSch and ChannelSftp classes are used to transfer the file on linux server.

    /**
     * Transfer file to Linux server 
     * @return true or false
     */
    public static boolean transferFileToLinuxMachine(String filePath){
    	JSch jsch = new JSch();
    	Session session = null;
    	try {
		// Put username and ip of the server
                session = jsch.getSession("admycsr","10.170.201.11",22);
		session.setPassword("csrtdev1");
	    	session.setConfig("StrictHostKeyChecking", "no");
	    	session.connect();
	    	System.out.println("server connected successfully");
	    	ChannelSftp channel = null;
	    	channel = (ChannelSftp)session.openChannel("sftp");
	    	channel.connect();
	    	System.out.println("channel connected successfully");
	    	File localFile = new File("C://Users//hlcz0172//Desktop//Noname2.txt");
	    	//If you want you can change the directory using the following line.
	    	channel.cd("/home/admycsr");
	    	channel.put(new FileInputStream(localFile),localFile.getName());
	    	System.out.println("File trasfered successfully");
	    	channel.disconnect();
	    	session.disconnect();
		} catch (JSchException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SftpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	return true;
    }