Introduction to sftp
sftp is the abbreviation of Secure File Transfer Protocol, a secure file transfer protocol. A secure network encryption method can be provided for transferring files. sftp has almost the same syntax and functions as ftp. SFTP is part of SSH and is a secure way to transfer files to a Blogger server. In fact, in the SSH package, a secure file information transfer subsystem called SFTP (Secure File Transfer Protocol) is already included. SFTP itself does not have a separate daemon. It must use the sshd daemon (the port number is 22 by default) to complete the corresponding connection and reply operations. Therefore, in a sense, SFTP is not like a server program, but more like a client program. SFTP also uses encryption to transmit authentication information and transmitted data, so it is very safe to use SFTP. However, because this transmission method uses encryption/decryption technology, 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.
Add dependencies
<dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.54</version></dependency>
Add configuration
sftp: ip: 192.168.1.60 port: 22 timeout: 60000 retryTime: 3 admin: username: admin password: 2k3xrYjbd930.
Code Example
Download csv files in multiple user directories at 1 a.m. every day to the local tmp directory
@Servicepublic class SftpTask extends Thread { private ChannelSftp sftp; private Session session; @Value("${sftp.admin.username}") private String username; @Value("${sftp.admin.password}") private String password; @Value("${sftp.host}") private String host; @Value("${sftp.port}") private Integer port; private SftpService sftpService; public EtlSftpTask (SftpService sftpService) { this.sftpService = sftpService; } /** * Establish a sftp connection*/ private void connect(){ try { JSch jSch = new JSch(); session = jSch.getSession(username, host, port); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); sftp = (ChannelSftp) channel; } catch (JSchException e) { e.printStackTrace(); } } /** * Close sftp connection*/ public void close(){ try { if (sftp != null) { if (sftp.isConnected()) sftp.disconnect(); } if(session != null){ if (session.isConnected()) session.disconnect(); } } catch (Exception e) { e.printStackTrace(); } } /** * Download the file to local * * @param source source file * @param target target file * @throws SftpException Exception * @throws FileNotFoundException Exception */ private void download(String source, String target) throws SftpException, FileNotFoundException { sftp.get(source, new FileOutputStream(new File(target))); } /** * Processing user data files* * @param root data file root directory* @param lastTime Last time when the last file was processed* @return The last time of processing the file*/ private Integer handle(String root, Integer lastTime) { String directory = root + "/event/"; Vector files; try { files = sftp.ls(directory + "event_*.csv"); } catch (Exception e) { e.printStackTrace(); return 0; } // File name String fileName; // Temporary file String tmpFile; // File update time Integer mTime; // File last update time Integer maxTime = lastTime; // Process user files for(Object o: files) { try { ChannelSftp.LsEntry f = (ChannelSftp.LsEntry) o; // File update time mTime = f.getAttrs().getMTime(); if (mTime <= lastTime) continue; // File name fileName = f.getFilename(); // Last process event maxTime = Math.max(maxTime, mTime); // Download file tmpFile = "/tmp/" + fileName; download(directory + fileName, tmpFile); } catch (Exception e) { // TODO error log e.printStackTrace(); } } // Return the last processing time of the file return maxTime; } /** * Execution starts at 1 a.m. every day*/ @Scheduled(cron = "0 0 1 * * *") public void task () { // Get sftp connection connect(); String root; Integer lastTime; Long cid; Integer maxTime = lastTime; // Get user list for (SftpDTO sftpDTO: sftpService.findAll()) { // User home directory root = sftpDTO.getSftpRoot(); // Last time of the last file processing lastTime = sftpDTO.getLastTime(); maxTime = Math.max(maxTime, handle(root, lastTime)); // Update the last processing time if (!maxTime.equals(lastTime)) { sftpDTO.setLastTime(maxTime); sftpService.update(sftpDTO); } } // Release sftp resource close(); }}Summarize
The above is the sample code for Java using sftp to download files regularly. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!