sftp.cd(AppLogDIR);
@Override public org.springframework.core.io.Resource downloadFile(String filename) { String HOST = "HOST@adresse"; int PORT = 22; String USER = "username"; String PASS = "password"; String AppLogDIR = "/path/to/your/remote/file/"; filename = null; try { JSch jsch = new JSch(); session = jsch.getSession(USER, HOST, PORT); session.setPassword(PASS); java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); channel = session.openChannel("sftp"); channel.connect(); sftp = (ChannelSftp) channel; sftp.cd(AppLogDIR); byte[] buffer = new byte[1024]; BufferedInputStream bis = new BufferedInputStream( sftp.get("remoteFile.log")); File newFile = new File("C:/remoteFile.log"); OutputStream os = new FileOutputStream(newFile); BufferedOutputStream bos = new BufferedOutputStream(os); int readCount; while ((readCount = bis.read(buffer)) > 0) { System.out.println("Writing: "); bos.write(buffer, 0, readCount); } bis.close(); bos.close(); } catch (Exception ex) { ex.printStackTrace(); } return newFile; } @GetMapping(value="/download/{filename}") public ResponseEntity<Object> downloadFile(@PathVariable String filename) { Resource file=sftpClient.downloadFile(filename); return ResponseEntity.ok() .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"") .body(file); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)