如何使用Web应用程序中的jsch连接到SFTP?

时间:2016-04-26 19:34:49

标签: java servlets sftp tomcat6 jsch

我绝不是专家程序员。 我目前的情况是这样的。 我构建了一个java项目,它使用jsch连接到SFTP服务器来发送/检索文件。要添加标识的代码中需要rsa私钥和密码。我还添加了known_hosts。一切正常,我能够连接到SFTP服务器并下载/上传文件。

接下来,我尝试将我的代码放入使用Tomcat的Maven项目中。所以我创建了带有几个按钮的jsp页面,然后使用java servlet将其发送出来以在服务器端执行我的代码。这是我遇到砖墙的地方。起初,我收到了nullpointerexceptions并发现它是由于权限设置。我不得不将一些目录/文件更改为chmod 777(我知道安全性真的很糟糕),但即使在那之后我也遇到了Auth Fail错误。我一直在网上搜索试图找到解决方案,但没有这样做。请帮助,非常感谢。

我不确定它是否重要,但客户端使用Windows连接到Web应用程序,而服务器正在运行UbuntU 14.04。

谢谢!

编辑:希望这已经足够了

在Web.XML中,我已经包含了

<servlet>
<servlet-name>sftpConn</servlet-name>
<servlet-class>package.sftpClass</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>sftpConn</servlet-name>
<url-pattern>/process</url-pattern>
</servlet-mapping>

在JSP页面

<%@ page import="java.util.*"%>
<jsp:useBean id="sftpConnection"
    class="package.sftpClass"
    scope="request" />

<html>
<head>
<title>sftp transfer</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/process" method="post">
    <input type="submit" name="Test" value="Test" />
</form>
</body>
</html>

最后在sftpClass.java中(我的所有变量都设置为private并使用Scanner从配置文件初始化)

public void connect () {

    String workingDir = "/transfer";

    Session session = null;
    Channel channel = null;
    ChannelSftp channelSftp = null;
    try {
        JSch jsch = new JSch();
        jsch.addIdentity(privateKey, privateKeyPassword);
        jsch.setKnownHosts(knownHosts);
        session = jsch.getSession(user, host, 22);
        session.setPassword(pass);
        session.connect();
        channel = session.openChannel("sftp");
        channel.connect();
        channelSftp = (ChannelSftp) channel;
        channelSftp.cd(workDir);
        File f = new File(filePath);
        channelSftp.put(new FileInputStream(f), f.getName());
    } catch (Exception ex) {
        // TODO Auto-generated catch block
        e.printstacktrace();        
    } finally {
        channelSftp.exit();
        channel.disconnect();
        session.disconnect();
    }
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    sftpClass s = new sftpClass();

    if (request.getParameter("Test") != null) {
        try {
            s.connect();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printstacktrace();
        }
    }
}

再次感谢你。

嗨,这是我从Catalina.out的堆栈跟踪日志中获得的内容

com.jcraft.jsch.JSchException: Auth fail
    at com.jcraft.jsch.Session.connect(Session.java:395)
    at com.jcraft.jsch.Session.connect(Session.java:148)
    at package.sftpClass.connect(sftpClass.java:1224)
    at package.sftpClass.create(sftpClass.java:820)
    at package.sftpClass.doPost(sftpClass.java:1337)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at oscar.oscarSecurity.LoginFilter.doFilter(LoginFilter.java:127)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at net.sf.cookierevolver.servlet.CRFilterImpl.doFilter(CRFilterImpl.java:60)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.oscarehr.util.LoggedInUserFilter.doFilter(LoggedInUserFilter.java:60)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.oscarehr.util.DbConnectionFilter.doFilter(DbConnectionFilter.java:65)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.oscarehr.util.ResponseDefaultsFilter.doFilter(ResponseDefaultsFilter.java:69)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.oscarehr.util.ProblemCheckFilter.doFilter(ProblemCheckFilter.java:188)
    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:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:662)

0 个答案:

没有答案