代码如下:
JSch jsch = new JSch();
String user = username;
String host = host;
int port = 22;
String privateKey = privateKeyPath;
jsch.addIdentity(privateKey);
Session sessionJ = jsch.getSession(user, host, port);
java.util.Properties configJ = new java.util.Properties();
configJ.put("StrictHostKeyChecking", "no");
sessionJ.setConfig(configJ);
sessionJ.connect();
Channel channel = sessionJ.openChannel("sftp");
channel.connect();
ChannelSftp cs = (ChannelSftp) channel;
out.print(cs.ls("D:/MyPath/mySubFolder"));
cs.exit();
channel.disconnect();
这就是我得到的:
3: Permission denied, file: /D:/myPath/mySubFolder at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2833) at com.jcraft.jsch.ChannelSftp._stat(ChannelSftp.java:2185) at com.jcraft.jsch.ChannelSftp._stat(ChannelSftp.java:2202) at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1566) at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1527) at org.apache.jsp.includes.shop.hubs.infinitiumSFTPConnection_jsp._jspService(infinitiumSFTPConnection_jsp.java:214) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:738) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at CharsetFilter.doFilter(CharsetFilter.java:39) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:724)
那是因为jsch ls期望相对路径。 如果我使用控制台连接到sftp,我会在C:\ Users \ user中提示 如何移动到D:而不是正确的路径? 目标是下载存储在D:/ myPath / mySubFolder
中的文件感谢您的帮助
编辑:
目前唯一的方法是在SFTP连接到D:音量后更改起始文件夹。
答案 0 :(得分:0)
对我来说(我正在使用jsch 0.1.54)可以在驱动器前添加“ /”,因此路径应类似于:
out.print(cs.ls("/D:/MyPath/mySubFolder"));
希望有帮助。