0

我正在尝试构建一个微服务,它将登录到某个远程服务器,并在该服务器中处理一些文件,并且对于异常,它将错误文件移动到当前目录的子目录中。我已经尝试过代码,但问题是我无法将文件复制到我通过代码传递的错误目录中。SFTP 服务器目录结构是

      \
      FTS     //Source directory 
       |
   FTSError  //error directory 

// Inside root we have FTS folder(Allthe files) and inside that FTSError folder we will place all the error files  

这是我尝试过的

try{
    fileName = fileName.substring(fileName.indexOf(".")-2;
    .........
   }catch(Exception ex){
    try{
    if(dirName.equals("FTS"){
    CopyFiles.copyFiles("FTS","FTSError",sftpChannel,fileName); /Here is a Util method i have called to pass the fileName,sftpChannel,source and target directory
    }
 }catch(SftpException sftpEx){
  logger.info("Exception occurred :",sftpEx.getMessage());  
 }
}

这是我写的 Util 方法

 public class CopyFiles{
 private static final Logger logger = LoggerFactory.getLogger(CopyFiles.class);
 public static copyFiles(String sourceLocation,String targetLocation,ChannelSftp sftpChannel,String originalFileName){

 SftpATTRS attrs = null;
 try{
  attrs = sftpChannel.stat("/"+sourceLocation+"/"+targetLocation);
  }catch(Exception e){
  logger.info(sourceLocation +" not Found");
 }
 if(attrs!=null){
  logger.info("Dircetory exits is"+attrs.isDir());
 }else{
  logger.info("creating dir "+targetLocation);
}


 transferToLocalErrorDirectory(originalFileName,sourceLocation,targetLocation,sftpChannel);
  }
 private static void transferToLocalErrorDirectory(String originalFile,String localDir,string  errorDirectory,ChannelSftp sftpChannel){
    // This OriginFile = the error file ex . JU.345_file.png
   // localDir = the Sourece dircetory  ex. FTS
   // errorDirectory  = the target error directory ex. FTSError
   // sftpChannel  = The channel sftp here 
   byte [] buffer = new byte[1024];
   BufferedInputStream bis = new BufferedInputStream(sftpChannel.get("\"+localDir+"\"+originalFile);
   File newFile = new File(localDir);
   OutputStream os = new FileOutputStream(os);
   BufferedOutputStream bos = new BufferedOutputStream(os);
   int readCount;
   while((readCount=bis.read(buffer))>0){
   bos.write(buffer,0,readCount);
   }
  logger.info("Completed :");
  bis.close();
  bos.close():
}  

但是我看不到将 JU.345_file.png 文件复制到 FTSError 目录中,我也没有遇到任何异常。我正在从下面的链接中获得帮助

在 JSCH 中创建新目录之前如何检查目录是否存在

4

0 回答 0