Click here to Skip to main content
15,892,072 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
sftp.cd(AppLogDIR);


Java
@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);
}


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 !
Posted
Updated 15-May-18 5:36am
v3
Comments
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