제목 그대로 StringBuffer에 담겨있는 녀석을 SFTP로 전송해야 하는 경우를 만났습니다.
에전에 FTP로는 해결했던 적이 있는데 SFTP는 처음이라 조금 애먹지 않을까 생각했는데 좋은 라이브러리와 샘플코드를 만나서 쉽게 해결했습니다.
- private Boolean ftpSend(StringBuffer sb) {
- String remotePath = "/web/geminidream";
- String filename= "test.xml";
- JSch jsch = new JSch();
- ArrayList<String> remoteServerIP = new ArrayList<String>();
- remoteServerIP.add("192.168.0.1");
- remoteServerIP.add("192.168.0.2");
- for(int i=0; i<remoteServerIP.size(); i++){
- String remoteIP = remoteServerIP.get(i);
- try {
- log.debug("start ftp send [" +remoteIP+ "]");
- Session session = jsch.getSession("user_id", remoteIP, 22);
- //log.debug("Session Created");
- session.setPassword("user_pw");
- java.util.Properties config = new java.util.Properties();
- config.put("StrictHostKeyChecking", "no");
- session.setConfig(config);
- session.connect();
- //log.debug("Session connected");
- //log.debug("opening channel");
- Channel channel=session.openChannel("sftp");
- channel.connect();
- ChannelSftp c=(ChannelSftp)channel;
- c.cd(remotePath);
- //c.cd("searchQueryLog");
- int mode=ChannelSftp.OVERWRITE;
- InputStream is = new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
- c.put(is, filename, mode);
- c.quit();
- session.disconnect();
- log.debug("end ftp");
- } catch(SftpException se) {
- log.debug("SftpException : " +se.fillInStackTrace());
- return false;
- } catch(Exception e){
- log.debug("Exception : " +e.fillInStackTrace());
- return false;
- }
- }
- return true;
- }
'Program is ... > with Web' 카테고리의 다른 글
[java + mysql] Insert하고 난 이후의 id값을 가져오기 (0) | 2009.10.08 |
---|---|
[Flex] dataProvider, ArrayCollection 그리고 Message 'warning: unable to bind to property ..' (3) | 2009.07.20 |
CSS - Layout 공부중.. (2) | 2008.12.18 |
JavaScript, form, return에 관한 이야기 (2) | 2008.10.08 |
웹 개발(Java)의 길은 점점 멀리간다.. (7) | 2008.09.23 |
JS로 AJAX처럼 하기 - Cross Domain 문제해결하기 - 2 (0) | 2008.08.11 |
JS로 AJAX처럼 하기 - Cross Domain 문제해결하기 - 1 (1) | 2008.08.08 |