sftp introduction

sftp is the abbreviation of Secure File Transfer Protocol, secure file transfer protocol. It can provide a secure network encryption method for transfer files. SFTP has almost the same syntax and function as FTP. SFTP is part of SSH, which is a way to transfer files to Blogger servers. In fact, in the SSH package, there is already a secure file information transmission subsystem called SFTP (Secure File Transfer Protocol), and SFTP itself does not have a separate daemon. It must use the sshd daemon (port number default is 22) to complete the corresponding connection and reply operation, so in a sense SFTP is not like a server program, but more like a client program. SFTP also uses encrypted transmission to authenticated information and transmit data, so the use of SFTP is very safe. However, because of the use of encryption / decryption technology in this way, the transmission efficiency is much lower than that of ordinary FTP. If you have higher network security requirements, you can use SFTP instead of FTP. The addition of

to

 < dependency> < groupId> com.jcraft< /groupId> < Rt: 22 timeout: 60000 retryTime: 3 admin: username: admin password: 2k3xrYjbd930.

code example

download CSV files to the local directory at 1 a.m. every day SFTP; private Session session; @Value ("${sftp.admin.username}") private String username; @Value ("${sftp.admin.password}") private. EtlSftpTask (SftpService sftpService) {this.sftpService = sftpService;} / * * * establish SFTP connection * / private void connect () {try {JSch jSch =}. RictHostKeyChecking "," no "); session.connect (); Channel channel = session.openChannel (" SFTP "); channel.connect (); SFTP = (ChannelSftp) channel;}catch ({} {});} / * * closed connection * / {} {} Y {if (SFTP! = null) {if (sftp.isConnected ()) sftp.disconnect ();} if (session! = null) {if (session.isConnected ());}}} {} {};}} / * * Download Text to local * The rce source file * @param target target file * @throws SftpException exception * @throws FileNotFoundException exception * / private void Download Ile (target)))));} / * * processing the user data file * @param root data file root * the last time of the last processing file of @param lastTime * the last time * / private Integer handle (String root) Root + "/event/"; Vector files; try {files = sftp.ls (directory + "event_*.csv");} catch (Exception E) {{}; {0;} / / / / / / / / / / / / / / file update time; / / / file last update time Integer maxTime = lastTime; / / / / / / / processing user file for (Object o: files) {try {ChannelSftp.LsEntry f = (ChannelSftp.LsEntry) O; / / / / file update time = =; / / / file name fileName = f.getFilename (); / / / final processing event maxTime = Math.max (maxTime, mTime); / / / / download file tmpFile = "/tmp/" + fileName; download (directory + fileName,);} {/ /} error day E.printStackTrace ();}}} / / / / / / / / / / / / / / / / * * / * * / * * * / * * * / * * / * @Scheduled (cron = "001 * * *") public void task () {/ / / / obtain SFTP connection connect ()) at 1 a.m.; String root; Long CID; Integer maxTime = lastTime; / / / / obtain the user list for (SftpDTO sftpDTO: sftpService.findAll ()) {/ / / / / / / / / user master directory root = sftpDTO.getSftpRoot (); / / / / last time for the last processing file = lastTime = {] Ndle (root, lastTime)); / / / / update final processing time if (! MaxTime.equals (lastTime)) {sftpDTO.setLastTime (maxTime); sftpService.update (sftpDTO);}} / / / / / release SFTP resource close ();} /p>

above is a sample code for Java to download files regularly with SFTP, and hope to help you. If you have any questions, please leave a message to me, and the small editor will respond to you in time. Thank you for your support for the script home website.

This paper fixed link:http://www.script-home.com/sample-code-for-java-to-download-files-regularly-with-sftp.html | Script Home | +Copy Link

Article reprint please specify:Sample code for Java to download files regularly with SFTP | Script Home

You may also be interested in these articles!