在访问linux时,首先需要使用工具类jar包:ganymed-ssh2
登录远程服务器:
public boolean login(){
//创建远程连接,默认连接端口为22,如果不使用默认,可以使用方法
//new Connection(ip, port)创建对象
Connection conn = new Connection(ip);
try {
//连接远程服务器
();
//使用用户名和密码登录
return (usr, psword);
} catch (IOException e) {
("用户%s密码%s登录服务器%s失败!", usr, psword, ip);
();
}
return false;
}
/**
* 上传本地文件到服务器目录下
* @param conn Connection对象
* @param fileName 本地文件
* @param remotePath 服务器目录
*/
public void putFile(Connection conn, String fileName, String remotePath){
SCPClient sc = new SCPClient(conn);
try {
//将本地文件放到远程服务器指定目录下,默认的文件模式为 0600,即 rw,
//如要更改模式,可调用方法 put(fileName, remotePath, mode),模式须是4位数字且以0开头
(fileName, remotePath);
} catch (IOException e) {
();
}
}
/**
* 下载服务器文件到本地目录
* @param fileName 服务器文件
* @param localPath 本地目录
*/
public void copyFile(Connection conn, String fileName,String localPath){
SCPClient sc = new SCPClient(conn);
try {
(fileName, localPath);
} catch (IOException e) {
();
}
}
/**
* 在远程LINUX服务器上,在指定目录下,获取文件各个属性
* @param[in] conn Conncetion对象
* @param[in] remotePath 远程主机的指定目录
*/
public void getFileProperties(Conncetion conn, String remotePath){
try {
SFTPv3Client sft = new SFTPv3Client(conn);
Vector<?> v = (remotePath);
for(int i=0;i<();i++){
SFTPv3DirectoryEntry s = new SFTPv3DirectoryEntry();
s = (SFTPv3DirectoryEntry) (i);
//文件名
String filename = ;
//文件的大小
Long fileSize = ;
}
();
} catch (Exception e1) {
();
}
}
/**
* 在远程LINUX服务器上,在指定目录下,删除指定文件
* @param[in] fileName 文件名
* @param[in] remotePath 远程主机的指定目录
* @return
*/
public void delFile(String remotePath, String fileName){
try {
SFTPv3Client sft = new SFTPv3Client(conn);
//获取远程目录下文件列表
Vector<?> v = (remotePath);
for(int i=0;i<();i++){
SFTPv3DirectoryEntry s = new SFTPv3DirectoryEntry();
s = (SFTPv3DirectoryEntry) (i);
//判断列表中文件是否与指定文件名相同
if((fileName)){
//rm()方法中,须是文件绝对路径+文件名称
(remotePath + );
}
();
} catch (Exception e1) {
();
}
}
/**
* 执行脚本
* @param conn Connection对象
* @param cmds 要在linux上执行的指令
*/
public int exec(Connection conn, String cmds){
InputStream stdOut = null;
InputStream stdErr = null;
int ret = -1;
try {
//在connection中打开一个新的会话
Session session = ();
//在远程服务器上执行linux指令
(cmds);
//指令执行结束后的输出
stdOut = new StreamGobbler(());
//指令执行结束后的错误
stdErr = new StreamGobbler(());
//等待指令执行结束
(ChannelCondition.EXIT_STATUS, TIME_OUT);
//取得指令执行结束后的状态
ret = ();
();
}catch(Exception e){
();
}
return ret;
}
自己写的代码
controller:
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
@RestController
@RequestMapping("/fileDownCtl")
public class FileDownController {
@Autowired
private FileUtil fileUtil;
@Autowired
private LinuxLogin linuxLogin;
@RequestMapping(value="/getFile",method=)
public void getFile(HttpServletResponse response){
String path = "D:/Games/WarCraft/BeeWind软件站说明.txt";
(path, response);
}
@RequestMapping(value="/getLinuxFile",method=)
public void getLinuxFile(HttpServletResponse response) throws IOException{
String fileName = "";
String path = "/home/hadoop/text/";
();
();
("bin");
("octets/stream");
("Content-Type", "text/html; charset=utf-8");
("Content-Disposition", "attachment;filename="+new String(("UTF-8"),"ISO8859-1"));
(, path+fileName,());
}
}
工具类:
package ;
import ;
import ;
import ;
import ;
import .;
import .;
import .ssh2.SFTPv3Client;
import .ssh2.SFTPv3DirectoryEntry;
@Component
public class LinuxLogin {
public static Connection conn = null;
public boolean login(){
//创建远程连接,默认连接端口为22,如果不使用默认,可以使用方法
//new Connection(ip, port)创建对象
conn = new Connection("192.168.159.128");
try {
//连接远程服务器
();
//使用用户名和密码登录
return ("hadoop", "hadoop");
} catch (IOException e) {
("用户%s密码%s登录服务器%s失败!", "hadoop", "hadoop", "192.168.159.128");
();
}
return false;
}
/**
* 复制到JAVA所在服务器
* @param conn
* @param fileName
* @param localPath
*/
public void copyFile(Connection conn, String fileName,String localPath){
SCPClient sc = new SCPClient(conn);
try {
(fileName, localPath);
} catch (IOException e) {
();
}
}
/**
* 流式输出,用于浏览器下载
* @param conn
* @param fileName
* @param outputStream
*/
public void copyFile(Connection conn, String fileName,ServletOutputStream outputStream){
SCPClient sc = new SCPClient(conn);
try {
(fileName, outputStream);
} catch (IOException e) {
();
}
}
/**
* 在远程LINUX服务器上,在指定目录下,获取文件各个属性
* @param[in] conn Conncetion对象
* @param[in] remotePath 远程主机的指定目录
*/
public void getFileProperties(Connection conn, String remotePath){
try {
SFTPv3Client sft = new SFTPv3Client(conn);
Vector<?> v = (remotePath);
for(int i=0;i<();i++){
SFTPv3DirectoryEntry s = new SFTPv3DirectoryEntry();
s = (SFTPv3DirectoryEntry) (i);
//文件名
String filename = ;
//文件的大小
Long fileSize = ;
}
();
} catch (Exception e1) {
();
}
}
public static void main(String args[]){
LinuxLogin log = new LinuxLogin();
(());
}
}
仅供测试参考
转自:/QQ156881887/article/details/80209864
/wangmuming/article/details/20537289