文件下载:仅通过 JSP 的 SFTP 连接
File download: SFTP connection via JSP only
我想创建 jsp,它将负责与 SFTP 服务器的连接,并且
然后显示指定路径下的文件(由用户输入)
此外,我想创建一个下载 link 或按钮,让用户可以从显示的文件列表中下载文件。
此文件随后将下载到 sftp 连接服务器(路径:如 "tmp" 文件夹中)或用户本地目录。
进度::
我已成功建立连接以及显示文件列表的代码。
但是,我一直在努力编码 button/link 让用户下载他们想要下载的文件。
<%@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>
现在,剩下的就是设置下载条件,并将文件放入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();
}
}
%>
那是,祝你在代码中按正确的顺序编译。
我想创建 jsp,它将负责与 SFTP 服务器的连接,并且 然后显示指定路径下的文件(由用户输入)
此外,我想创建一个下载 link 或按钮,让用户可以从显示的文件列表中下载文件。
此文件随后将下载到 sftp 连接服务器(路径:如 "tmp" 文件夹中)或用户本地目录。
进度:: 我已成功建立连接以及显示文件列表的代码。 但是,我一直在努力编码 button/link 让用户下载他们想要下载的文件。
<%@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>
现在,剩下的就是设置下载条件,并将文件放入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();
}
}
%>
那是,祝你在代码中按正确的顺序编译。