channelsftp的put,SFTP ChannelSftp.put停止执行过程,但已成功上传或复制了源文件

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.

解决方案

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.)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值