当前位置: 代码迷 >> java >> 使用SFTP上传后,本地文件未删除
  详细解决方案

使用SFTP上传后,本地文件未删除

热度:75   发布时间:2023-07-16 17:35:49.0

通过SSH将文件从本地计算机传输到远程ubuntu计算机后,我想在本地计算机上删除文件。 文件已成功发送,但是未删除。 请帮忙

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();
    }
}
File f = new File("C:/Users/khun/testFile.txt");
try {
    channelSftp.put(new FileInputStream(f), f.getName());
} catch ...

您正在打开从文件读取的FileInputStream,然后再也不要关闭它。 Windows通常不允许删除当前打开的文件。 打开文件时,需要考虑确保最终关闭文件。 使用try-with-resources语法,您可以这样做:

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

使用这种语法,当控件离开try块时,java将自动关闭流。

问题可能是流。 流已经打开了吗? 我不知道channelSftp.exit(); channel.disconnect(); session.disconnect(); 也关闭流。 (从未与Jsch合作)

您可以尝试手动关闭它吗?

例如:

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

删除返回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返回true。