簡體   English   中英

如何使用 Java 將文件從 linux 服務器發送到 windows 服務器?

[英]How to send file from linux server to windows server using Java?

我在使用 Java 將文件從 Linux 服務器發送到 windows 服務器時遇到問題。 我使用 jsch 庫來發送文件。 當我從 linux 服務器對 windows 服務器的 990 端口進行遠程登錄時,一切正常。 這是它的外觀:

在此處輸入圖像描述

我寫了這段代碼來發送文件:

protected boolean sendFileToRemoteServer() {
    String fileName = redExpressConfigurationProperties.getUploadPath() + "test.txt";
    JSch jsch = new JSch();
    Session jschSession = null;
    boolean returnedValue = false;
    try {
        jschSession = getSessionOfRemoteServer(jsch);
        log.debug("Got Session!");
        ChannelSftp channelSftp = getSftpChannelFromSession(jschSession);

        channelSftp.put(fileName, redExpressConfigurationProperties.getRemoteServerFilePath());
        channelSftp.exit();
        log.debug("File sent!");
        returnedValue = true;
    } catch (JSchException | SftpException e) {
        log.error("Exception: ", e);
    } finally {
        if (jschSession != null) {
            jschSession.disconnect();
        }
    }
    return returnedValue;
}

protected Session getSessionOfRemoteServer(JSch jsch) throws JSchException {
    Properties properties = new Properties();
    properties.put("StrictHostKeyChecking", "no");
    Session session = jsch.getSession(redExpressConfigurationProperties.getRemoteServerUser(),
            redExpressConfigurationProperties.getRemoteServerAddress(),
            redExpressConfigurationProperties.getRemoteServerSshPort()); // port is 990
    session.setConfig(properties);
    session.setPassword(redExpressConfigurationProperties.getRemoteServerPassword());
    log.debug("password: {}", redExpressConfigurationProperties.getRemoteServerPassword());
    log.debug("server address: {}", redExpressConfigurationProperties.getRemoteServerAddress());
    return session;
}

protected ChannelSftp getSftpChannelFromSession(Session jschSession) throws JSchException {
    jschSession.connect(redExpressConfigurationProperties.getRemoteSessionTimeout()); // timeout is 10000 
    log.debug("Server Connected!");
    Channel channel = jschSession.openChannel("sftp");
    channel.connect(redExpressConfigurationProperties.getRemoteChannelTimeout());
    log.debug("Got sftp channel for session!");
    return (ChannelSftp) channel;
}

我的 gradle 依賴是這樣的:

implementation 'com.jcraft:jsch:0.1.55'

這是我的 application.properties 文件的一部分:

在此處輸入圖像描述

但是當我運行代碼時,我在嘗試連接 windows 服務器時遇到異常。 這是日志:

2020-12-21 17:48:07:976 [main] ERROR c.e.d.DemoApplication:78 - Exception: 
com.jcraft.jsch.JSchException: Session.connect: java.net.SocketTimeoutException: Read timed out
    at com.jcraft.jsch.Session.connect(Session.java:565)
    at com.example.demo.DemoApplication.getSftpChannelFromSession(DemoApplication.java:55)
    at com.example.demo.DemoApplication.sendFileToRemoteServer(DemoApplication.java:71)
    at com.example.demo.DemoApplication.test(DemoApplication.java:31)
    at com.example.demo.DemoApplication$$EnhancerBySpringCGLIB$$a080bb04.CGLIB$test$3(<generated>)
    at com.example.demo.DemoApplication$$EnhancerBySpringCGLIB$$a080bb04$$FastClassBySpringCGLIB$$22565bf1.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
    at com.example.demo.DemoApplication$$EnhancerBySpringCGLIB$$a080bb04.test(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:486)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1179)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:571)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:531)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:923)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:588)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:326)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298)
    at com.example.demo.DemoApplication.main(DemoApplication.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:107)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)

這是我遇到異常的行:

jschSession.connect(redExpressConfigurationProperties.getRemoteSessionTimeout());

我做錯了什么? 我是否選擇了錯誤的圖書館來達到我的目的? 是否有任何身份驗證問題?或任何其他問題?

我沒有通過谷歌搜索找到太多幫助。 所以貼在這里。 能得到幫助會很棒。 提前致謝。

我在使用 Java 將文件從 Linux 服務器發送到 windows 服務器時遇到問題。 我使用 jsch 庫來發送文件。 當我從 linux 服務器對 windows 服務器的 990 端口進行遠程登錄時,一切正常。 這是它的外觀:

在此處輸入圖像描述

我寫了這段代碼來發送文件:

protected boolean sendFileToRemoteServer() {
    String fileName = redExpressConfigurationProperties.getUploadPath() + "test.txt";
    JSch jsch = new JSch();
    Session jschSession = null;
    boolean returnedValue = false;
    try {
        jschSession = getSessionOfRemoteServer(jsch);
        log.debug("Got Session!");
        ChannelSftp channelSftp = getSftpChannelFromSession(jschSession);

        channelSftp.put(fileName, redExpressConfigurationProperties.getRemoteServerFilePath());
        channelSftp.exit();
        log.debug("File sent!");
        returnedValue = true;
    } catch (JSchException | SftpException e) {
        log.error("Exception: ", e);
    } finally {
        if (jschSession != null) {
            jschSession.disconnect();
        }
    }
    return returnedValue;
}

protected Session getSessionOfRemoteServer(JSch jsch) throws JSchException {
    Properties properties = new Properties();
    properties.put("StrictHostKeyChecking", "no");
    Session session = jsch.getSession(redExpressConfigurationProperties.getRemoteServerUser(),
            redExpressConfigurationProperties.getRemoteServerAddress(),
            redExpressConfigurationProperties.getRemoteServerSshPort()); // port is 990
    session.setConfig(properties);
    session.setPassword(redExpressConfigurationProperties.getRemoteServerPassword());
    log.debug("password: {}", redExpressConfigurationProperties.getRemoteServerPassword());
    log.debug("server address: {}", redExpressConfigurationProperties.getRemoteServerAddress());
    return session;
}

protected ChannelSftp getSftpChannelFromSession(Session jschSession) throws JSchException {
    jschSession.connect(redExpressConfigurationProperties.getRemoteSessionTimeout()); // timeout is 10000 
    log.debug("Server Connected!");
    Channel channel = jschSession.openChannel("sftp");
    channel.connect(redExpressConfigurationProperties.getRemoteChannelTimeout());
    log.debug("Got sftp channel for session!");
    return (ChannelSftp) channel;
}

我的 gradle 依賴是這樣的:

implementation 'com.jcraft:jsch:0.1.55'

這是我的 application.properties 文件的一部分:

在此處輸入圖像描述

但是當我運行代碼時,我在嘗試連接 windows 服務器時遇到異常。 這是日志:

2020-12-21 17:48:07:976 [main] ERROR c.e.d.DemoApplication:78 - Exception: 
com.jcraft.jsch.JSchException: Session.connect: java.net.SocketTimeoutException: Read timed out
    at com.jcraft.jsch.Session.connect(Session.java:565)
    at com.example.demo.DemoApplication.getSftpChannelFromSession(DemoApplication.java:55)
    at com.example.demo.DemoApplication.sendFileToRemoteServer(DemoApplication.java:71)
    at com.example.demo.DemoApplication.test(DemoApplication.java:31)
    at com.example.demo.DemoApplication$$EnhancerBySpringCGLIB$$a080bb04.CGLIB$test$3(<generated>)
    at com.example.demo.DemoApplication$$EnhancerBySpringCGLIB$$a080bb04$$FastClassBySpringCGLIB$$22565bf1.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
    at com.example.demo.DemoApplication$$EnhancerBySpringCGLIB$$a080bb04.test(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:486)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1179)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:571)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:531)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:923)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:588)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:326)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298)
    at com.example.demo.DemoApplication.main(DemoApplication.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:107)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)

這是我遇到異常的行:

jschSession.connect(redExpressConfigurationProperties.getRemoteSessionTimeout());

我做錯了什么? 我是否選擇了錯誤的圖書館來達到我的目的? 是否有任何身份驗證問題?或任何其他問題?

我沒有通過谷歌搜索找到太多幫助。 所以貼在這里。 能得到幫助會很棒。 提前致謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM