ssh删除文件

This commit is contained in:
Arno 2019-10-25 16:03:33 +08:00
parent 12ed8e7190
commit 7dfd54ffa5
2 changed files with 91 additions and 0 deletions

View File

@ -239,4 +239,72 @@ public class SshFileController extends BaseServerController {
JschUtil.close(session); JschUtil.close(session);
} }
} }
@RequestMapping(value = "delete.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
@Feature(method = MethodFeature.DEL)
public String delete(String id, String path, String name) {
SshModel sshModel = sshService.getItem(id);
if (sshModel == null) {
return JsonMessage.getString(400, "ssh error");
}
List<String> fileDirs = sshModel.getFileDirs();
//
if (StrUtil.isEmpty(path) || !fileDirs.contains(path)) {
return JsonMessage.getString(405, "没有配置此文件夹");
}
if (StrUtil.isEmpty(name)) {
return JsonMessage.getString(400, "name error");
}
Session session = null;
ChannelSftp channel = null;
try {
String normalize = FileUtil.normalize(path + "/" + name);
session = sshService.getSession(sshModel);
channel = (ChannelSftp) JschUtil.openChannel(session, ChannelType.SFTP);
deleteFile(channel, normalize);
return JsonMessage.getString(200, "删除成功");
} catch (Exception e) {
DefaultSystemLog.getLog().error("ssh删除文件异常", e);
return JsonMessage.getString(400, "删除失败");
} finally {
JschUtil.close(channel);
JschUtil.close(session);
}
}
/**
* 删除文件或文件夹
*
* @param channel channel
* @param path 文件路径
* @throws SftpException SftpException
*/
private void deleteFile(ChannelSftp channel, String path) throws SftpException {
Vector<ChannelSftp.LsEntry> vector = channel.ls(path);
if (null == vector) {
return;
}
if (vector.size() == 1) {
// 文件直接删除
channel.rm(path);
} else if (vector.size() == 2) {
// 空文件夹直接删除
channel.rmdir(path);
} else {
// 删除文件夹下所有文件
for (ChannelSftp.LsEntry en : vector) {
String fileName = en.getFilename();
if (".".equals(fileName) || "..".equals(fileName)) {
continue;
} else {
deleteFile(channel, path + "/" + fileName);
}
}
// 删除文件夹
channel.rmdir(path);
}
}
} }

View File

@ -125,6 +125,7 @@
elem: '#tree', elem: '#tree',
data: [], data: [],
id: "tree", id: "tree",
edit: ['del'],
click: function (obj) { click: function (obj) {
var data = obj.data; var data = obj.data;
if (!data.dir) { if (!data.dir) {
@ -151,8 +152,30 @@
return; return;
} }
getData(nowPath, data.parentDir, data); getData(nowPath, data.parentDir, data);
},
operate: function (obj) {
if ('del' === obj.type) {
deleteFile(obj.data);
}
} }
}); });
//删除文件
function deleteFile(data) {
loadingAjax({
url: './delete.json',
data: {
id: id,
path: nowPath,
name: data.parentDir
},
success: function (data) {
layer.msg(data.msg);
allData = false;
getData(nowPath, '');
}
});
}
}); });
$("#download").click(function () { $("#download").click(function () {