文件下载:仅通过 JSP 进行 SFTP 连接



我想创建jsp,它将负责与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 中实现这些代码,以便用户专门下载文件。

添加了隐藏元素,然后解析它们的值。

<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>

现在,rest正在设置下载文件并将其放入sftp临时目录的条件

这将添加到 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();
}
}
%>

也就是说,在代码中按正确的顺序编译是好运。

最新更新