问题
details:
in my API i have struggle on debugging why is that the ChannelSftp.put method hangs up or stop it's execution process but when checking it's output it is successfully being uploaded.
here's my code snippet:
MyService.class
@Inject
MyConfiguration conf;
public String copyAndMove( String fileName ){
try{
MyServer origin = conf.getOriginServer().setFileName( fileName );
MyServer destination = conf.getDestinationServer().setFileName( fileName );
SFTPServer originSftpServer = new SFTPServer( origin ).build();
SFTPServer destinationSftpServer = new SFTPServer( destination ).build();
// originSftpServer.copyTo(destinationSftpServer);
originSftpServer.copyTo(originSftpServer);
return "Successfully copied file.";
}catch( Exception ex ){
throw new IllegalStateException(ex.getMessage(), ex);
}
}
SFTPServer.class
public class SFTPServer {
private MyServer server;
private static SFTPServer instance;
private Session session = null;
private Channel channel = null;
private ChannelSftp channelSftp = null;
// getters and setters
public SFTPServer(){}
public SFTPServer(MyServer server) throws Exception{
if(CommonUtil.isNull( server )){
throw new Exception("MyServer cannot be null!");
}
this.server = server;
}
public SFTPServer build(){
try{
this.session = SFTPUtil.constructSession(getServer());
this.channel = SFTPUtil.constructChannel(getSession());
this.channelSftp = (ChannelSftp) channel;
return this;
} catch (Exception ex) {
throw new IllegalStateException(ex.getMessage(), ex);
}
}
public SFTPServer copyTo( SFTPServer destination ) {
InputStream is = null;
try{
ChannelSftp channel = destination.getChannelSftp();
String originSourceFile = String.format("%s/%s", getServer().getSourceFilePath(), getServer().getFileName());
String destinationProcessedFile = String.format("%s/%s", destination.getServer().getProcessedFilePath(), destination.getServer().getFileName());
is = getChannelSftp().get(originSourceFile);
channel.put(is, destinationProcessedFile, ChannelSftp.OVERWRITE);
return this;
} catch (Exception ex) {
throw new IllegalStateException(ex.getMessage(), ex);
}finally{
CommonUtil.closeQuitely(is); // close input stream
destination.destroy(); // disconnect session, channel, channelSftp
}
}
}
The problem is this. it's seems that it cannot proceed until the program done for it's execution. when i debug on it it stop and it's execution for this code: channel.put(is, destinationProcessedFile, ChannelSftp.OVERWRITE); but on the sftp server it is successfully being copied from source to destination file path. please help me with this problem because it cannot return the Successfully copied file. on the service part. thanx.
回答1:
As I've not able figure this one out using input streams, absolute vs relative pathings, here is an alternative:
Instead of using put, use rename
try
{
channelSftp.cd(destinationDirectory);
channelSftp.rename(pathToFile1, pathToFile2);
channelSftp.cd(currentDirectory);
} catch (SftpException e)
{
e.printStackTrace();
}
Basically it renames a file from one directory to another. Move to the destination directory, use rename to create that file there, and change back to the directory you were in.
(I would've rather used the .put method, but ran into the same issue.)
来源:https://stackoverflow.com/questions/36026454/sftp-channelsftp-put-stop-its-execution-process-but-successfully-being-uploaded