heja,
jest taka sytuacja: pisze prosty program, ktory miedzy innymi ma za zadanie wysłać plik na serwer sftp i skasować następnie z dysku plik, który wysłano.... i tu pojawia się problem, ponieważ plik nie chce się usunąć:
new TestJSch(computer_name); //(1)
File f = new File("C://" + computer_name + ".txt");
System.out.println(f.getAbsolutePath());
f.delete();
bez linijki (1) wysyłającej plik na serwer, plik się usunie.
Oto kod TestJSch.java :
import java.io.File;
import java.io.FileInputStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
class TestJSch {
public TestJSch(String computer_name) {
try {
String ftpHost = "sftp.pjwstk.edu.pl";
int ftpPort = 22;
String ftpUserName = "X";
String ftpPassword = "Y";
String ftpRemoteDirectory = "Z";
String fileToTransmit = "C://" + computer_name + ".txt";
System.out.println("Creating session.");
JSch jsch = new JSch();
Session session = null;
Channel channel = null;
ChannelSftp c = null;
try {
session = jsch.getSession(ftpUserName, ftpHost, ftpPort);
System.out.println("Session created.");
session.setPassword(ftpPassword);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
System.out.println("Session connected.");
System.out.println("Opening Channel.");
channel = session.openChannel("sftp");
channel.connect();
c = (ChannelSftp)channel;
} catch (Exception e) {
System.err.println("Unable to connect to FTP server. "+e.toString());
throw e;
}
System.out.println("Changing to FTP remote dir: " + ftpRemoteDirectory);
c.cd(ftpRemoteDirectory);
try {
File f = new File(fileToTransmit);
System.out.println("Storing file as remote filename: " + f.getName());
c.put(new FileInputStream(f), f.getName());
} catch (Exception e) {
System.err.println("Storing remote file failed. "+e.toString());
throw e;
}
try {
c.quit();
session.disconnect();
} catch (Exception exc) {
System.err.println("Unable to disconnect from FTP server. " + exc.toString());
}
} catch (Exception e) {
System.err.println("Error: "+e.toString());
}
System.out.println("Process Complete.");
}
}