<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>build210</version>
</dependency>
public boolean login(){
Connection conn = new Connection(ip);
try {
conn.connect();
return conn.authenticateWithPassword(usr, psword);
} catch (IOException e) {
System.err.printf("用户%s密码%s登录服务器%s失败!", usr, psword, ip);
e.printStackTrace();
}
return false;
}
public void putFile(Connection conn, String fileName, String remotePath){
SCPClient sc = new SCPClient(conn);
try {
sc.put(fileName, remotePath);
} catch (IOException e) {
e.printStackTrace();
}
}
public void copyFile(Connection conn, String fileName,String localPath){
SCPClient sc = new SCPClient(conn);
try {
sc.get(fileName, localPath);
} catch (IOException e) {
e.printStackTrace();
}
}
public void getFiles(String remoteDir, String localTargetDirectory) {
Connection conn = new Connection(ip, port);
Connection conn2 = getConn2(ip, port, username, password);
SFTPv3Client sft = null;
try {
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
if (!isAuthenticated) {
System.err.println("authentication failed");
}
SCPClient client = new SCPClient(conn);
sft = new SFTPv3Client(conn2);
Vector<?> v = sft.ls(remoteDir);
for (int i = 0; i < v.size(); i++) {
SFTPv3DirectoryEntry s = (SFTPv3DirectoryEntry) v.get(i);
if (fileType(s.filename)) {
client.get(remoteDir + "/" + s.filename, localTargetDirectory);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (sft != null) {
sft.close();
}
if (conn != null) {
conn.close();
}
}
}
public void getFileProperties(Conncetion conn, String remotePath){
try {
SFTPv3Client sft = new SFTPv3Client(conn);
Vector<?> v = sft.ls(remotePath);
for(int i=0;i<v.size();i++){
SFTPv3DirectoryEntry s = new SFTPv3DirectoryEntry();
s = (SFTPv3DirectoryEntry) v.get(i);
String filename = s.filename;
Long fileSize = s.attributes.size;
}
sft.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
public void delFile(String remotePath, String fileName){
try {
SFTPv3Client sft = new SFTPv3Client(conn);
Vector<?> v = sft.ls(remotePath);
for(int i=0;i<v.size();i++){
SFTPv3DirectoryEntry s = new SFTPv3DirectoryEntry();
s = (SFTPv3DirectoryEntry) v.get(i);
if(s.filename.equals(fileName)){
sft.rm(remotePath + s.filename);
}
sft.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
public int exec(Connection conn, String cmds){
InputStream stdOut = null;
InputStream stdErr = null;
int ret = -1;
try {
Session session = conn.openSession();
session.execCommand(cmds);
stdOut = new StreamGobbler(session.getStdout());
stdErr = new StreamGobbler(session.getStderr());
session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);
ret = session.getExitStatus();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
return ret;
}
package com.sun.redis.controller;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.sun.redis.util.FileUtil;
import com.sun.redis.util.LinuxLogin;
@RestController
@RequestMapping("/fileDownCtl")
public class FileDownController {
@Autowired
private FileUtil fileUtil;
@Autowired
private LinuxLogin linuxLogin;
@RequestMapping(value="/getFile",method=RequestMethod.GET)
public void getFile(HttpServletResponse response){
String path = "D:/Games/WarCraft/BeeWind软件站说明.txt";
fileUtil.getListFileList(path, response);
}
@RequestMapping(value="/getLinuxFile",method=RequestMethod.GET)
public void getLinuxFile(HttpServletResponse response) throws IOException{
String fileName = "linux.txt";
String path = "/home/hadoop/text/";
linuxLogin.login();
response.reset();
response.setContentType("bin");
response.setContentType("octets/stream");
response.addHeader("Content-Type", "text/html; charset=utf-8");
response.addHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("UTF-8"),"ISO8859-1"));
linuxLogin.copyFile(linuxLogin.conn, path+fileName,response.getOutputStream());
}
}
package com.sun.redis.util;
import java.io.IOException;
import java.util.Vector;
import javax.servlet.ServletOutputStream;
import org.springframework.stereotype.Component;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.SFTPv3Client;
import ch.ethz.ssh2.SFTPv3DirectoryEntry;
@Component
public class LinuxLogin {
public static Connection conn = null;
public boolean login(){
conn = new Connection("192.168.159.128");
try {
conn.connect();
return conn.authenticateWithPassword("hadoop", "hadoop");
} catch (IOException e) {
System.err.printf("用户%s密码%s登录服务器%s失败!", "hadoop", "hadoop", "192.168.159.128");
e.printStackTrace();
}
return false;
}
public void copyFile(Connection conn, String fileName,String localPath){
SCPClient sc = new SCPClient(conn);
try {
sc.get(fileName, localPath);
} catch (IOException e) {
e.printStackTrace();
}
}
public void copyFile(Connection conn, String fileName,ServletOutputStream outputStream){
SCPClient sc = new SCPClient(conn);
try {
sc.get(fileName, outputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public void getFileProperties(Connection conn, String remotePath){
try {
SFTPv3Client sft = new SFTPv3Client(conn);
Vector<?> v = sft.ls(remotePath);
for(int i=0;i<v.size();i++){
SFTPv3DirectoryEntry s = new SFTPv3DirectoryEntry();
s = (SFTPv3DirectoryEntry) v.get(i);
String filename = s.filename;
Long fileSize = s.attributes.size;
}
sft.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
public static void main(String args[]){
LinuxLogin log = new LinuxLogin();
System.out.println(log.login());
}
}