Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
J
jiayanWMS
Project
Project
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
Graph
比较
统计图
议题
0
议题
0
列表
看板
标记
Milestones
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
李炎
jiayanWMS
Commits
a2c80c68
提交
a2c80c68
authored
9月 16, 2021
作者:
许俊
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修改
上级
f9dee2b5
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
180 行增加
和
20 行删除
+180
-20
RemoteCommandUtil.java
...va/org/jeecg/modules/iost/API/Util/RemoteCommandUtil.java
+139
-0
DownLoadController.java
.../modules/iost/basedata/controller/DownLoadController.java
+41
-20
没有找到文件。
jeecg-boot-module-system/src/main/java/org/jeecg/modules/iost/API/Util/RemoteCommandUtil.java
0 → 100644
浏览文件 @
a2c80c68
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
;
}
}
jeecg-boot-module-system/src/main/java/org/jeecg/modules/iost/basedata/controller/DownLoadController.java
浏览文件 @
a2c80c68
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");*/
// 创建服务对象
OutputStream
outputStream
=
new
BufferedOutputStream
(
response
.
getOutputStream
());
RemoteCommandUtil
commandUtil
=
new
RemoteCommandUtil
();
//创建存放文件内容的数组
// 登录到LInux服务
byte
[]
buff
=
new
byte
[
1024
];
Connection
root
=
commandUtil
.
login
(
"192.168.2.225"
,
"root"
,
"Ll666888"
);
//所读取的内容使用n来接收
// 服务器文件路径
int
n
;
String
fwPath
=
"/home"
;
//当没有读取完时,继续读取,循环
// windows本地存放目录,不需要加上具体文件
while
((
n
=
in
.
read
(
buff
))
!=
-
1
)
{
String
bdPath
=
"D:\\file\\"
;
//将字节数组的数据全部写入到输出流中
// 下载文件
outputStream
.
write
(
buff
,
0
,
n
);
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
());
byte
[]
b
=
new
byte
[
4096
];
int
i
;
while
((
i
=
in
.
read
(
b
))
!=
-
1
)
{
outputStream
.
write
(
b
,
0
,
i
);
}
outputStream
.
flush
();
outputStream
.
close
();
in
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
commandUtil
.
closeConn
();
}
}
//制将缓存区的数据进行输出
outputStream
.
flush
();
//关流
outputStream
.
close
();
in
.
close
();
}
}
/*未使用-下载apk*/
/*未使用-下载apk*/
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论