代码如下:
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: 卷后更改起始文件夹。