提交 a2c80c68 authored 作者: 许俊's avatar 许俊

修改

上级 f9dee2b5
package org.jeecg.modules.iost.API.Util;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.SCPInputStream;
import ch.ethz.ssh2.SCPOutputStream;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
@Slf4j
public class RemoteCommandUtil {
private static int port = 22;
private static Connection conn = null;
public static Connection login(String ip, String userName, String userPwd) {
boolean flg = false;
Connection conn = null;
try {
conn = new Connection(ip,port);
//连接
conn.connect();
//认证
flg = conn.authenticateWithPassword(userName, userPwd);
if (flg) {
log.info("连接成功");
return conn;
}
} catch (IOException e) {
log.error("连接失败" + e.getMessage());
e.printStackTrace();
}
return conn;
}
/**
* 实现下载服务器上的文件到本地指定目录
*
* @param conn SSH连接信息
* @param basePath 服务器上的文件地址/home/img
* @param localPath 本地路径:D:\
* @throws IOException
*/
/* public void downloadFile(Connection conn, String fileName, String basePath, String localPath) throws IOException {
SCPClient scpClient = conn.createSCPClient();
try {
SCPInputStream sis = scpClient.get(basePath + "/" + fileName);
File f = new File(localPath);
if (!f.exists()) {
f.mkdirs();
}
File newFile = new File(localPath + fileName);
FileOutputStream fos = new FileOutputStream(newFile);
byte[] b = new byte[4096];
int i;
while ((i = sis.read(b)) != -1) {
fos.write(b, 0, i);
}
fos.flush();
fos.close();
sis.close();
log.info("下载完成");
} catch (IOException e) {
log.info("文件不存在或连接失败");
e.printStackTrace();
} finally {
log.info("服务关闭");
closeConn();
}
}*/
public SCPInputStream downloadFile(Connection conn, String fileName, String basePath, String localPath) throws IOException {
SCPClient scpClient = conn.createSCPClient();
SCPInputStream sis = scpClient.get(basePath + "/" + fileName);
File f = new File(localPath);
if (!f.exists()) {
f.mkdirs();
}
File newFile = new File(localPath + fileName);
FileOutputStream fos = new FileOutputStream(newFile);
byte[] b = new byte[4096];
int i;
while ((i = sis.read(b)) != -1) {
fos.write(b, 0, i);
}
return sis;
}
/**
* 上传文件到服务器
*
* @param conn SSH连接信息
* @param f 文件对象
* @param remoteTargetDirectory 上传路径
* @param mode 默认为null
*/
public void uploadFile(Connection conn, File f, String remoteTargetDirectory, String mode) {
try {
SCPClient scpClient = new SCPClient(conn);
SCPOutputStream os = scpClient.put(f.getName(), f.length(), remoteTargetDirectory, mode);
byte[] b = new byte[4096];
FileInputStream fis = new FileInputStream(f);
int i;
while ((i = fis.read(b)) != -1) {
os.write(b, 0, i);
}
os.flush();
fis.close();
os.close();
log.info("上传成功");
} catch (IOException e) {
e.printStackTrace();
} finally {
closeConn();
}
}
/**
* 关闭服务
*/
public static void closeConn() {
if (null == conn) {
return;
}
conn.close();
}
public ByteArrayInputStream parse(final OutputStream out) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos = (ByteArrayOutputStream) out;
final ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
return swapStream;
}
}
package org.jeecg.modules.iost.basedata.controller; package org.jeecg.modules.iost.basedata.controller;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.SCPInputStream;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.PropertiesUtil; import org.apache.logging.log4j.util.PropertiesUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions; import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.aspect.annotation.AutoLog; import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.iost.API.Util.FileUtil; import org.jeecg.modules.iost.API.Util.FileUtil;
import org.jeecg.modules.iost.API.Util.RemoteCommandUtil;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
...@@ -53,30 +57,47 @@ public class DownLoadController { ...@@ -53,30 +57,47 @@ public class DownLoadController {
@AutoLog(value = "下载APK2") @AutoLog(value = "下载APK2")
@ApiOperation(value = "下载APK2", notes = "下载APK2") @ApiOperation(value = "下载APK2", notes = "下载APK2")
@GetMapping("/download2.apk") @GetMapping("/test.apk")
public void download2(HttpServletRequest request, HttpServletResponse response) throws IOException { public void download2(HttpServletRequest request, HttpServletResponse response) throws IOException {
/* InputStream in=getClass().getClassLoader().getResourceAsStream("res/test.apk");*/
response.setContentType("application/vnd.android.package-archive"); response.setContentType("application/vnd.android.package-archive");
//读取文件
File file = new File("/home/test.apk");
byte[] body = null; byte[] body = null;
InputStream in = new FileInputStream(file); /*InputStream in = new FileInputStream(file);*/
/*InputStream in = getClass().getClassLoader().getResourceAsStream("file/test.apk");*/ // 创建服务对象
RemoteCommandUtil commandUtil = new RemoteCommandUtil();
// 登录到LInux服务
Connection root = commandUtil.login("192.168.2.225", "root", "Ll666888");
// 服务器文件路径
String fwPath = "/home";
// windows本地存放目录,不需要加上具体文件
String bdPath = "D:\\file\\";
// 下载文件
SCPClient scpClient = root.createSCPClient();
try {
SCPInputStream sis = scpClient.get(fwPath + "/" + "test.apk");
File f = new File(bdPath);
if (!f.exists()) {
f.mkdirs();
}
InputStream in=new BufferedInputStream(sis);
File newFile = new File(bdPath + "test.apk");
OutputStream fos = new FileOutputStream(newFile);
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream()); OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
//创建存放文件内容的数组 byte[] b = new byte[4096];
byte[] buff = new byte[1024]; int i;
//所读取的内容使用n来接收 while ((i = in.read(b)) != -1) {
int n; outputStream.write(b, 0, i);
//当没有读取完时,继续读取,循环
while ((n = in.read(buff)) != -1) {
//将字节数组的数据全部写入到输出流中
outputStream.write(buff, 0, n);
} }
//制将缓存区的数据进行输出
outputStream.flush(); outputStream.flush();
//关流
outputStream.close(); outputStream.close();
in.close(); in.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
commandUtil.closeConn();
}
} }
/*未使用-下载apk*/ /*未使用-下载apk*/
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论