다운로드



public static void download(String filePath, String fileName, 
         BufferedOutputStream bos) throws Exception {
   String IP   = GetProperties.getProperty("FTP.IP"); // 서버 ip
   String ID   = GetProperties.getProperty("FTP.ID"); // ftp 접속 id
   String PWD  = GetProperties.getProperty("FTP.PWD"); // ftp 접속 비밀번호
   String PORT = GetProperties.getProperty("FTP.PORT"); // ftp 포트
   String PATH = GetProperties.getProperty("FTP.PATH")+"/"+filePath; // ftp 경로
   ////System.out.println("PATH :: "+PATH);
         Session session = null;
         Channel channel = null;
         ChannelSftp channelSftp = null;
         BufferedInputStream bis  = null;
         try {
             JSch jsch = new JSch();
             session = jsch.getSession(ID, IP, Integer.parseInt(PORT));
             session.setPassword(PWD);
             java.util.Properties config = new java.util.Properties();
             config.put("StrictHostKeyChecking", "no");
             session.setConfig(config);
             session.connect();
             channel = session.openChannel("sftp");
             channel.connect();
             channelSftp = (ChannelSftp) channel;
             
             channelSftp.cd(GetProperties.filePathBlackList(PATH));
             
             //System.out.println(channelSftp.pwd());
             fileName = fileName.replace("//", "/");
             
             System.out.println(PATH);
             System.out.println(fileName);
             System.out.println(GetProperties.filePathBlackList(PATH)+fileName);
             System.out.println(channelSftp.pwd());
             System.out.println(channelSftp.get(GetProperties.filePathBlackList(PATH)+fileName));
             
             bis = new BufferedInputStream(channelSftp.get(GetProperties.filePathBlackList(PATH)+fileName));
             FileCopyUtils.copy(bis, bos);
             bos.flush(); 
           
         } catch (Exception ex) {
//          noimage2.jpg
          //System.out.println("시스템 아웃");
          bis = new BufferedInputStream(channelSftp.get("/media/smama/2018/noimage1.jpg"));
          //System.out.println("시스템 아웃");
             ex.printStackTrace();
         }finally {
          bis.close();
             bos.close();
    session.disconnect();
   }
 }




업로드


public static int upload(InputStream is, String remoteFilePath, String fileName) {
   InputStream fis = is; // File Input Stream
      int result = -1;
     String IP   = GetProperties.getProperty("FTP.IP"); // 서버 ip
   String ID   = GetProperties.getProperty("FTP.ID"); // ftp 접속 id
   String PWD  = GetProperties.getProperty("FTP.PWD"); // ftp 접속 비밀번호
   String PORT = GetProperties.getProperty("FTP.PORT"); // ftp 포트
   String PATH = GetProperties.getProperty("FTP.PATH")+"/"+remoteFilePath; // ftp 경로
   
         Session session = null;
         Channel channel = null;
         ChannelSftp channelSftp = null;
        
         System.out.println(PATH);
         System.out.println(PATH);
         System.out.println(PATH);
         System.out.println(PATH);
         System.out.println(remoteFilePath);

         try {
             JSch jsch = new JSch();
             session = jsch.getSession(ID, IP, Integer.parseInt(PORT));
             session.setPassword(PWD);
             java.util.Properties config = new java.util.Properties();
             config.put("StrictHostKeyChecking", "no");
             session.setConfig(config);
             session.connect();
             channel = session.openChannel("sftp");
             channel.connect();
             channelSftp = (ChannelSftp) channel;
             
             System.out.println(PATH);
             
             SftpATTRS attrs=null;
             try {
             
                 attrs = channelSftp.stat(PATH);
             } catch (Exception e) {
              e.printStackTrace();
                 System.out.println(PATH+" not found");
             }

             if (attrs != null) {
                 System.out.println("Directory exists IsDir="+attrs.isDir());
                 channelSftp.cd(PATH);
             } else {
              mkdirs(PATH,channelSftp);
                 channelSftp.cd(PATH);
             }
             
             System.out.println(" pwd :: "+channelSftp.pwd());
             System.out.println(" fileName :: "+fileName);
             
              channelSftp.put(is, fileName);
              result = 1;
         } catch (Exception ex) {
             ex.printStackTrace();
         }
    return result;  
 }



폴더 생성



private static void mkdirs(String directory, ChannelSftp c) throws IOException, SftpException {
     try {
         SftpATTRS att = c.stat(directory);
         if (att != null) {
             if (att.isDir()) {
                 return;
             }
         }
     } catch (SftpException ex) {
         if (directory.indexOf('/') != -1) {
             mkdirs(directory.substring(0, directory.lastIndexOf('/')), c);
             System.out.println(directory);
         }
         System.out.println(directory);
         
         c.mkdir(directory);
     }
 }