我想通过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();
}
}
答案 0 :(得分:1)
File f = new File("C:/Users/khun/testFile.txt");
try (FileInputStream stream = new FileInputStream(f)) {
channelSftp.put(stream, f.getName());
} catch ...
您正在从文件中打开FileInputStream读取,然后从不关闭它。 Windows通常不允许删除当前打开的文件。当您打开文件时,您需要考虑确保它最终关闭。使用try-with-resources语法,您可以这样做:
try
使用此语法,当控件离开{{1}}块时,java将自动关闭流。
答案 1 :(得分:0)
问题可能是流。流已经打开了吗? 我不知道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());
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。