0

I am trying to build a microservice which will login to some remote server and in that server it will process some files and for an exception it will move the error file into a subdirectory of the present directory. I have tried the code , but the problem is i can not copy the file into the error directory which i am passing through the code.The SFTP server directory structure is

      \
      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  

Here is what i have tried

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());  
 }
}

Here is the Util method i have written

 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():
}  

But i can't see the JU.345_file.png file copied into FTSError directory , i am not getting any exception also. I was taking help from the below link

How to check directory is existed before creating a new directory in JSCH

Mandrek
  • 1,115
  • 3
  • 17
  • 45
  • Your code does not even attempt to upload any file. There's not a single call to `ChannelSftp.put`. See [SFTP file transfer using Java JSch](https://stackoverflow.com/q/15108923/850848). – Martin Prikryl Jan 27 '22 at 19:21

0 Answers0