Click here to Skip to main content
15,670,482 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone
I'm trying to download files from an sftp server and i didn't know how to make it work
I'll show you my code and tell where did i go wrong :)

What I have tried:

I tried it with the code below and when i test it with postman i got 404 not found as a response :
maybe it means that it didn't find the file to donwload ?
focus on the lines after;

    public 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);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            channel = session.openChannel("sftp");
            sftp = (ChannelSftp) channel;
            byte[] buffer = new byte[1024];
            BufferedInputStream bis = new BufferedInputStream(
            File newFile = new File("C:/remoteFile.log"); OutputStream os = new FileOutputStream(newFile);
            BufferedOutputStream bos = new BufferedOutputStream(os);
            int readCount;
            while ((readCount = > 0) {
                System.out.println("Writing: ");
                bos.write(buffer, 0, readCount);
        } catch (Exception ex) {
        return newFile;

public ResponseEntity<Object> downloadFile(@PathVariable String filename) {
    Resource file=sftpClient.downloadFile(filename);
    return ResponseEntity.ok()
            .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"")

i really googled and searched for a solution but none of them solved my problem this is why i m coming back to you guys

I'll be so thankful for your help !
Updated 15-May-18 5:36am
Richard Deeming 15-May-18 11:23am    
If you want someone to help you fix a problem, then you need to tell us what the problem is!

Click the green "Improve question" link and update your question with a proper description of the problem. Include the full details of any errors, and remember to indicate which line of code they relate to.
Member 13828418 15-May-18 11:35am    
here you go , i updated my question :)
Richard Deeming 15-May-18 11:40am    
I'm not familiar with Spring, but a 404 error would suggest to me that the request isn't being mapped to your endpoint properly.
Member 13828418 15-May-18 11:48am    
I'll try my best to find where the problem is :) thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900