user3820292
user3820292

Reputation: 310

Local file not deleting after having been uploaded with SFTP

I want to delete a file at my local machine after transferring the file from my local machine to my remote ubuntu machine via SSH. The file is sent successfully but however the file is not deleting. Please help

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

public class Scp {

    public Scp() {

    }

    public static void transfer() {
        String SFTPHOST = "ip address";
        int SFTPPORT = 22;
        String SFTPUSER = "username";
        String SFTPPASS = "password";
        String SFTPWORKINGDIR = "workingDirPath";

        Session session = null;
        Channel channel = null;
        ChannelSftp channelSftp = null;
        System.out.println("preparing the host information for sftp.");
        JSch jsch = new JSch();
        try {
            session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
            //session.connect(30000);
        } catch (JSchException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        session.setPassword(SFTPPASS);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        try {
            session.connect();
        } catch (JSchException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("Host connected.");
        try {
            channel = session.openChannel("sftp");
        } catch (JSchException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            channel.connect();
        } catch (JSchException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("sftp channel opened and connected.");
        try {
            channelSftp = (ChannelSftp) channel;
            System.out.println(SFTPWORKINGDIR);
            channelSftp.cd(SFTPWORKINGDIR); 
        } catch (SftpException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //String path = System.getProperty("user.home");
        File f = new File("C:/Users/khun/testFile.txt");
        try {

            channelSftp.put(new FileInputStream(f), f.getName());

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SftpException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("File transfered successfully to host.");
        channelSftp.exit();
        System.out.println("sftp Channel exited.");
        channel.disconnect();
        System.out.println("Channel disconnected.");
        session.disconnect();
        System.out.println("Host Session disconnected.");

        f.getAbsolutePath();
        if(f.delete()) {
            System.out.println("File is deleted");
        }
    }

    public static void main(String[] args) {
        transfer();
    }
}

Upvotes: 1

Views: 1660

Answers (2)

Kenster
Kenster

Reputation: 25439

File f = new File("C:/Users/khun/testFile.txt");
try {
    channelSftp.put(new FileInputStream(f), f.getName());
} catch ...

You're opening a FileInputStream reading from the file, and then never closing it. Windows typically doesn't permit deleting a file that is currently open. When you open a file, you need to think about making sure it is eventually closed. Using the try-with-resources syntax, you could do this:

File f = new File("C:/Users/khun/testFile.txt");
try (FileInputStream stream = new FileInputStream(f)) {
    channelSftp.put(stream, f.getName());
} catch ...

With this syntax, java will automatically close the stream when control leaves the try block.

Upvotes: 1

pL4Gu33
pL4Gu33

Reputation: 2085

The Problem can be the Stream. Is the stream already open? I dont know if channelSftp.exit(); channel.disconnect();session.disconnect(); close the Stream, too. (Never worked with Jsch)

Can you try to close it manually?

for example:

File f = new File("test.txt");
f.createNewFile();
System.out.println(f.exists());
InputStream stream = new FileInputStream(f);
System.out.println(f.delete());

delete returns false.

File f = new File("test.txt");
f.createNewFile();
System.out.println(f.exists());
InputStream stream = new FileInputStream(f);
stream.close();
System.out.println(f.delete());

delete returns true.

Upvotes: 0

Related Questions