简体   繁体   English

使用ganymed-ssh2将Java scp文件远程

[英]java scp file to remote using ganymed-ssh2

I am having a situation here which I need to resolve. 我在这里需要解决一种情况。 I have to upload particular elements of an xml file to upload it to a server, managed to do that, and I created a demo method to check if the file is being uploaded to the server or not. 我必须上载xml文件的特定元素以将其上载到服务器,并设法做到这一点,并且我创建了一个演示方法来检查文件是否正在上载到服务器。

My xml file has the structure, 我的xml文件具有以下结构:

<config>
 <engine>
    <eid>1</eid>
    <sometextelement>text</sometextelement>
 </engine>

 <engine>
    <eid>2</eid>
    <sometextelement>text</sometextelement>
 </engine>

 <engine>
    <eid>3</eid>
    <sometextelement>text</sometextelement>
 </engine>


</config>

and here is my servlet code: 这是我的servlet代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request,response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("application/json");
        //response.setHeader("Cache-Control", "no-cache");
         PrintWriter out = response.getWriter();
         JSONObject obj = new JSONObject();
         String value = request.getParameter("value");
            String message = "";
            String update = "";
            Element element = null;
            Element root = null;
            XMLOutputter xmlOutputter = new XMLOutputter();
        try{
        doc = saxBuilder.build("E:/workbench j2ee/cPEP_UI/WebContent/engine.xml");

        }catch(Exception e){
            e.printStackTrace();
        }
        root = doc.getRootElement();
        List list = doc.getRootElement().getChildren();
        Iterator itr = list.iterator();
        while(itr.hasNext()){
             element = (Element)itr.next();
            System.out.println("Entered 1");
          File f = File.createTempFile("engine_",".xml");
           System.out.println(f);
            xmlOutputter.output(element, new FileWriter(f));

            putFile(f);
        }



         // xmlOutputter.output(doc, new FileWriter("E:/workbench j2ee/cPEP_UI/WebContent/engine.xml"));





        // System.out.println("hello from system");
        // out.println("hello");



    }

    public void putFile(File f){
         System.out.println("File String: "+f.toString());
         Connection Conn = null;
          try {
           Conn = new Connection("ftp.someserver.co.uk",22);
           ConnectionInfo info = Conn.connect();
           Conn.authenticateWithPassword("webmaster@someserver.co.uk", "mypass");
           SCPClient SCP = new SCPClient(Conn);
           SCP.put(f.toString(), "/public_html/webmaster", "0755");
           Conn.close();
          } catch (Exception e) {
           try {
            Conn.close();
           } catch (Exception e1) {}
    }
    }

Now, From the above code I am getting file created for engine 1, engine 2 and engine 3, and thats what I wanted, but now I want it to upload to the server too. 现在,从上面的代码中,我正在为引擎1,引擎2和引擎3创建文件,这就是我想要的,但是现在我也希望它也上传到服务器。 and the putFile code is not working at all. 并且putFile代码根本不起作用。 No error is being displayed too, pass and user name are all fine, and the server name too. 也不会显示任何错误,密码和用户名都可以,服务器名称也可以。 The port is also perfect. 该港口也很完美。 Whats wrong then? 那怎么了 any ideas? 有任何想法吗?

Edit- 1 编辑-1

Heres the error: 继承人错误:

java.io.IOException: There was a problem while connecting to ftp.someserver.co.uk:22
    at ch.ethz.ssh2.Connection.connect(Connection.java:699)
    at ch.ethz.ssh2.Connection.connect(Connection.java:490)
    at Push_Individual_Engine.putFile(Push_Individual_Engine.java:106)
    at Push_Individual_Engine.doPost(Push_Individual_Engine.java:81)
    at Push_Individual_Engine.doGet(Push_Individual_Engine.java:47)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    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.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:859)
    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:662)
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:529)
    at ch.ethz.ssh2.transport.TransportManager.establishConnection(TransportManager.java:340)
    at ch.ethz.ssh2.transport.TransportManager.initialize(TransportManager.java:448)
    at ch.ethz.ssh2.Connection.connect(Connection.java:643)
    ... 18 more

您需要在远程计算机的sshd_config文件中设置PasswordAuthentication = yes

The probable reason why no error is being displayed is that you are discarding Exceptions . 没有显示错误的可能原因是您正在丢弃Exceptions You should expect to get "doesn't work, but no error is being displayed" problems if you discard Exceptions. 如果您丢弃异常,您应该期望得到“不起作用,但没有显示错误”的问题。 Instead, you should probably print a stack trace in the catch blocks, like this: 相反,您可能应该在catch块中打印堆栈跟踪,如下所示:

e.printStackTrace();

or log the exception, or re-throw the exception. 或记录异常,或重新引发异常。 Those are the three standard choices. 这些是三个标准选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM