我想创建一个负责与SFTP服务器和 然后在指定的路径(由用户输入)中显示文件
此外,我想创建一个下载链接或按钮,使用户可以从显示的文件列表中下载文件。
此文件将被下载到sftp连接器服务器(路径:类似“ tmp”文件夹中)或用户本地目录中。
进度:: 我已经成功建立了连接以及代码以显示文件列表。 但是,我一直在努力对按钮/链接进行编码,使用户可以下载他们想要下载的文件。
<%@page import="java.util.Collections"%>
<%@page import="org.apache.commons.io.*"%>
<%@page import="java.io.File"%>
<%@page import="java.io.File"%>
<%@page import="com.jcraft.jsch.*"%>
<%@page import="java.util.Vector"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<script>
function download(fn) {
alert("downloading: " + fn);
}
</script>
<body>
<%
String filname;
String username = request.getParameter("username");
if (null == username)
username = "";
String password = request.getParameter("password");
if (null == password)
password = "";
String host = request.getParameter("host");
if (null == host) {
host = "";
}
String portStr = request.getParameter("port");
if (null == portStr)
portStr = "";
int port = 0;
try {
port = Integer.parseInt(portStr);
} catch (Exception ex) {
}
String folder = request.getParameter("folder");
if (null == folder) {
folder = "";
String download = request.getParameter("dwnld");
boolean flag = false;
if (download != null) {
flag = true;
}
}
%>
<form method="POST">
<table>
<tr>
<th align="right">SFTPHOST</th>
<td><input name="host" value=<%=host%> size="50" type="text"></td>
</tr>
<tr>
<th align="right">Username</th>
<td><input name="username" value="<%=username%>"></td>
</tr>
<tr>
<th align="right">Password</th>
<td><input name="password" value="<%=password%>"
type="password"></td>
</tr>
<tr>
<th>Port</th>
<td><input type="number" name="port" size="4" value="<%=port%>" /></td>
</tr>
<tr>
<th>Working Directory</th>
<td><input name="folder" value="<%=folder%>" size="100" /></td>
</tr>
<p style="color: red">Please provide full path</p>
<tr>
<td colspan="2"><input type="submit" /></td>
</tr>
</table>
</form>
<%
if ("".equals(username.trim()) && port >= 0 && "".equals(host.trim()) && "".equals(password.trim())) {
%>
<%
return;
}
JSch jsch = new JSch();
Session sessionJsch = null;
try {
sessionJsch = jsch.getSession(username, host, port);
sessionJsch.setConfig("StrictHostKeyChecking", "no");
sessionJsch.setPassword(password);
System.out.println("aapass: " + password);
System.out.println("Establishing Connection...");
sessionJsch.connect();
System.out.println("Connection established.");
System.out.println("Creating SFTP Channel.");
Channel channel = sessionJsch.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
System.out.println("SFTP Channel created.");
%>
<%
Vector ls = sftpChannel.ls(folder);
%>
<table border="1" cellpadding="1" cellspacing="3">
<tr>
<th>Name</th>
<th>Recieved Time</th>
</tr>
<%
for (Object entry : ls) {
ChannelSftp.LsEntry e = (ChannelSftp.LsEntry) entry;
System.out.println(e.getFilename());
SftpATTRS attrs = e.getAttrs();
%>
<tr>
<td><%=e.getFilename()%> <a
href="javascript:download('<%=e.getFilename()%>')">download</a></td>
<td><%=attrs.getMtimeString()%></td>
</tr>
<%
}
%>
</table>
<%
sftpChannel.exit();
} finally {
if (sessionJsch != null) {
sessionJsch.disconnect();
}
}
%>
</body>
</html>
我将分享我要在jsp或通过ajax实现的下载代码, 因为一旦页面加载,整个jsp就会执行,所以我确定我必须在ajax中实现那些代码,以便用户专门下载文件。
答案 0 :(得分:0)
什么都不感谢,但是解决了。
添加了隐藏元素,然后解析它们的值。
<script>
function dwnld(fuck) {
alert("downloading: " + fuck);
var run = "666";
document.forms[0]['dwnldfile'].value=fuck;
document.forms[0]['run'].value=run;
document.forms[0].submit();
}
</script>
<%
String run = request.getParameter("run");
if (null == run)
run = "";
String dwnldfile = request.getParameter("dwnldfile");
if (null == dwnldfile)
dwnldfile = "";
%>
<tr>
<td><input name="dwnldfile" value="<%=dwnldfile%> "type="hidden" />
</td>
</tr>
<tr>
<td><input name="run" value="<%=run%>" type="hidden" /></td>
</tr>
正在显示的地方,添加下面的代码
<td><%="<a href='javascript:dwnld(\"" + folder + File.separator + "/" + e.getFilename()
+ "\");'>dld</a>"%></td>
现在,剩下的就是设置下载条件并将文件放入sftp的temp目录中的条件
将添加到for循环中
<%
if (a.equals("666")) {
File file = new File(dwnldfile);
System.out.print("Download Successfull: " + file.getName());
PipedInputStream pin = new PipedInputStream(2048);
PipedOutputStream pout = new PipedOutputStream(pin);
sftpChannel.cd("/tmp");
String fout = "/tmp/" + file.getName();
sftpChannel.put(pin, fout);
a = "69 fuck";
System.out.println("finish");
pin.close();
pout.close();
}
}
%>
也就是说,以正确的顺序在代码中进行编译很幸运。
P.S:您不敢删除我的答案。