Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

자기 혐오 개발자

[Java]Java에서 SFTP로 파일 전송 및 리눅스 명령어 실행 본문

Java,JSP

[Java]Java에서 SFTP로 파일 전송 및 리눅스 명령어 실행

올라치노 2018. 8. 2. 18:10

private final String svrIp = "127.0.0.1";

private final String user = "아이디";

private final String passwd = "비번";




/**

* FTP 전송 테스트.

* @param id

* @param model

* @param svcIdx

* @return

* @throws IOException

*/

@RequestMapping(value = "/test/ftpTransferingTesting", method = RequestMethod.GET)

public String ftpTransferingTesting() throws IOException {

Channel channel = null;

ChannelSftp sftpChannel = null;

Session session = null;

String result = "1";

String rDir = "/폴더1/폴2/test_folder";

String lDir = "C:\\download";

StringBuffer sb = new StringBuffer();

try {

// 채널 생성.

session = returnSession(svrIp, user, passwd);

channel = session.openChannel("sftp");

channel.connect();


sftpChannel = (ChannelSftp) channel;

sftpChannel.cd(rDir);

// sftpChannel.get("estt.txt", lDir + "/" + "estt.txt");

                  // 현재 로컬서버의 파일을, 원격 리눅스 서버에 복사한다.

sftpChannel.put(lDir + "/" + "test_file_from_local.txt", "test_file_from_local_3.txt");


                        // 원격 리눅스 서버의 ls 명령어를 수행한 후 결과를 보낸다. 

Vector filelist = sftpChannel.ls(rDir);

for (int i = 0; i < filelist.size(); i++) {

sb.append(filelist.get(i).toString() + "\n\r");

}


model.addAttribute("result", result);

model.addAttribute("result_txt", sb.toString());

} catch (Exception e) {

sftpChannel.disconnect();

session.disconnect();

e.printStackTrace();

model.addAttribute("result", "0");

model.addAttribute("result_txt", "싪");

} finally {

sftpChannel.disconnect();

session.disconnect();

}

return "jsonView";

}


        /**

        * 명령어 보내기

        */  

        @RequestMapping(value = "/test/ftpCommandingTesting", method = RequestMethod.GET)

public String ftpCommandingTesting(@RequestParam(value = "command_line", defaultValue = "") String command_line,

Model model) throws IOException {

System.out.println(" TEST 1234 = " + command_line);

Session session = null;

String result = "1";

ChannelExec channelExec = null;

StringBuffer sb = new StringBuffer();

try {

// 채널 생성.

session = returnSession(svrIp, user, passwd);

String sudoCommand = command_line;

channelExec = (ChannelExec) session.openChannel("exec");

InputStream in = channelExec.getInputStream();

 // 명령어 수행.

channelExec.setCommand(sudoCommand);


channelExec.connect();


BufferedReader reader = new BufferedReader(new InputStreamReader(in));

String line;

int index = 0;


                        // 결과를 읽어 들인다.

while ((line = reader.readLine()) != null) {

System.out.println(line + "\n");

sb.append(line + "\n");

}


int exitStatus = channelExec.getExitStatus();

channelExec.disconnect();

session.disconnect();

if (exitStatus < 0) {

System.out.println("Done, but exit status not set!");

} else if (exitStatus > 0) {

System.out.println("Done, but with error!");

} else {

System.out.println("Done!");

}


model.addAttribute("result", result);

model.addAttribute("result_txt", sb.toString());

} catch (Exception e) {

channelExec.disconnect();

e.printStackTrace();

model.addAttribute("result", "0");

model.addAttribute("result_txt", "실패");

} finally {

channelExec.disconnect();

}

return "jsonView";

}




// session 생성.

private Session returnSession(String svrIp, String user, String passwd) throws Exception {

Session session = null;

try {

JSch jsch = new JSch();

session = jsch.getSession(user, svrIp, 22);

session.setPassword(passwd);

session.setConfig("StrictHostKeyChecking", "no");


session.connect();

} catch (Exception e) {

session.disconnect();

e.printStackTrace();

}


return session;

}





참조 : https://stackoverflow.com/questions/11532890/running-linux-commands-on-java-through-jsch