-
sftp java 접속 라이브러리 jschJava/Java Library 2021. 1. 27. 18:04
SFTP 접속하는 라이브러리는 jsch 다.
sftp: <http://www.jcraft.com/jsch/>
내서버에서 다른 서버 22번 sftp 접속하는 방법이다.
public class FTPUtil { private static Log logger = LogFactory.getLog(FTPUtil.class); private Session session = null; private Channel channel = null; private ChannelSftp channelSftp = null; // SFTP 서버연결 public void connect(String user, String url, String password) throws Exception{ logger.info(url); //JSch 객체 생성 JSch jsch = new JSch(); try { //세션객체 생성 ( user , host, port ) logger.info("session"); session = jsch.getSession(user, url); //password 설정 session.setPassword(password); //세션관련 설정정보 설정 java.util.Properties config = new java.util.Properties(); //호스트 정보 검사하지 않는다. config.put("StrictHostKeyChecking", "no"); session.setConfig(config); //접속 session.connect(); //sftp 채널 접속 channel = session.openChannel("sftp"); channel.connect(); } catch (JSchException e) { logger.error("connect JSchException error : " +e.toString()); throw new Exception("JSchException"); } catch (Exception e) { logger.error("connect error : " +e.toString()); throw new Exception(e.toString()); } channelSftp = (ChannelSftp) channel; } // 단일 파일 업로드 public void upload( String dir , File file){ FileInputStream in = null; try{ //파일을 가져와서 inputStream에 넣고 저장경로를 찾아 put in = new FileInputStream(file); channelSftp.cd(dir); channelSftp.put(in,file.getName()); }catch(SftpException se){ se.printStackTrace(); }catch(FileNotFoundException fe){ fe.printStackTrace(); }finally{ try{ in.close(); } catch(IOException ioe){ ioe.printStackTrace(); } } } public ChannelSftp moveDirectory(String dir){ try{ //경로탐색후 inputStream에 데이터를 넣음 channelSftp.cd(dir); // 결로 이동 }catch(SftpException se){ se.printStackTrace(); } return channelSftp; } /** * * @param dir 서버의 저장되어 있는 경로 * @param fileNm 다운받으려는 파일명 * @param saveDir 현 서버의 다운받으려고 하는 경로 */ public void download(String dir, String fileNm, String saveDir){ InputStream in = null; FileOutputStream out = null; //String path = "..."; try{ //경로탐색후 inputStream에 데이터를 넣음 channelSftp.cd(dir); // 결로 이동 in = channelSftp.get(fileNm); }catch(SftpException se){ logger.error("download " +se.toString()); } try { out = new FileOutputStream(new File(saveDir)); int i; while ((i = in.read()) != -1) { out.write(i); } } catch (IOException e) { logger.error("download io error : " +e.toString()); } finally { try { out.close(); in.close(); } catch (IOException e) { logger.error("download close error : " +e.toString()); } } } /** * 인자로 받은 경로의 파일 리스트를 리턴한다. * @param path * @return */ public Vector<ChannelSftp.LsEntry> getFileList(String path, String lsCmd) { Vector<ChannelSftp.LsEntry> list = null; try { channelSftp.cd(path); list = channelSftp.ls(lsCmd); } catch (SftpException e) { e.printStackTrace(); return null; } return list; } // 파일서버와 세션 종료 public void disconnect(){ channelSftp.quit(); session.disconnect(); } }
반응형'Java > Java Library' 카테고리의 다른 글