Spring Boot 如何使用Java Sping Boot 设置简单的SMB服务器?

mm9b1k5b  于 2023-04-30  发布在  Spring
关注(0)|答案(1)|浏览(286)

我有一个新的Sping Boot 项目。我想设置一个简单的SMB服务器,我可以连接到我的Windows资源管理器。我只是想共享一个文件夹,并能够连接到服务器设置的用户名和密码。我想分享的文件夹是D:\smb-files\folder1
我配置了SMC配置:

package com.example.smb;

import java.io.File;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.Pollers;
import org.springframework.integration.file.FileHeaders;
import org.springframework.integration.file.support.FileExistsMode;
import org.springframework.integration.smb.dsl.Smb;
import org.springframework.integration.smb.session.SmbSessionFactory;

import jcifs.DialectVersion;

@Configuration
public class SmbConfig {
    
    public static final String REMOTE_DIRECTORY = "folder1";

    @Bean
    public SmbSessionFactory smbSessionFactory() {
        SmbSessionFactory smbSession = new SmbSessionFactory();
        smbSession.setHost("myHost");
        smbSession.setPort(445);
        smbSession.setDomain("myDomain");
        smbSession.setUsername("myUser");
        smbSession.setPassword("myPassword");
        smbSession.setShareAndDir("myShareAndDir");
        smbSession.setSmbMinVersion(DialectVersion.SMB210);
        smbSession.setSmbMaxVersion(DialectVersion.SMB311);
        return smbSession;
    }

    @Bean
    public IntegrationFlow smbInboundFlow() {
        
        return IntegrationFlow
            .from(Smb.inboundAdapter(smbSessionFactory())
                    .preserveTimestamp(true)
                    .remoteDirectory(REMOTE_DIRECTORY)
                    .regexFilter(".*\\.txt$")
                    .localFilename(f -> f.toUpperCase() + ".a")
                    .localDirectory(new File("d:/smb-files")),
                        e -> e.id("smbInboundAdapter")
                    .autoStartup(true)
                    .poller(Pollers.fixedDelay(5000)))
            .handle(m -> System.out.println(m.getPayload()))
            .get();
    }
    
    @Bean
    public IntegrationFlow smbOutboundFlow() {
        return IntegrationFlow.from("toSmbChannel")
                .handle(Smb.outboundAdapter(smbSessionFactory(), FileExistsMode.REPLACE)
                        .useTemporaryFileName(false)
                        .fileNameExpression("headers['" + FileHeaders.FILENAME + "']")
                        .remoteDirectory(REMOTE_DIRECTORY)
                ).get();
    }

    @MessagingGateway
    public interface MyGateway {

         @Gateway(requestChannel = "toSmbChannel")
         void sendToSmb(File file);
    }
}

我试图尽可能接近这个Spring文档。我添加了一个全局字符串“REMOTE_DIRECTORY”,并将localDirectory更改为“d:/smb-files”。(我在我的磁盘上创建了这个目录,甚至测试了向目录中写一个简单的文件-〉它工作了)。我在smb-files中创建了一个名为folder1的文件夹。
我在pom.xml中添加了以下依赖项:

<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-smb</artifactId>
</dependency>

我配置了Spring Integrations以在DEBUG级别(应用程序。产品特性:

logging.level.org.springframework.integration=DEBUG

错误:

简而言之:Problem occurred while synchronizing 'folder1' to local directory
应用程序日志:

[payload=org.springframework.messaging.MessagingException: Problem occurred while synchronizing 'folder1' to local directory, headers={id=8a317e74-0c7e-129e-c7b2-e9a7a73a7be5, timestamp=1682786110723}]
2023-04-29T18:35:10.724+02:00 DEBUG 4300 --- [   scheduling-1] o.s.integration.handler.LoggingHandler   : bean '_org.springframework.integration.errorLogger.handler' for component '_org.springframework.integration.errorLogger' received message: ErrorMessage [payload=org.springframework.messaging.MessagingException: Problem occurred while synchronizing 'folder1' to local directory, headers={id=8a317e74-0c7e-129e-c7b2-e9a7a73a7be5, timestamp=1682786110723}]
2023-04-29T18:35:10.725+02:00 ERROR 4300 --- [   scheduling-1] o.s.integration.handler.LoggingHandler   : org.springframework.messaging.MessagingException: Problem occurred while synchronizing 'folder1' to local directory
    at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer.synchronizeToLocalDirectory(AbstractInboundFileSynchronizer.java:348)
    at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizingMessageSource.doReceive(AbstractInboundFileSynchronizingMessageSource.java:267)
    at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizingMessageSource.doReceive(AbstractInboundFileSynchronizingMessageSource.java:69)
    at org.springframework.integration.endpoint.AbstractFetchLimitingMessageSource.doReceive(AbstractFetchLimitingMessageSource.java:47)
    at org.springframework.integration.endpoint.AbstractMessageSource.receive(AbstractMessageSource.java:142)
    at org.springframework.integration.endpoint.SourcePollingChannelAdapter.receiveMessage(SourcePollingChannelAdapter.java:212)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint.doPoll(AbstractPollingEndpoint.java:443)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint.pollForMessage(AbstractPollingEndpoint.java:412)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint.lambda$createPoller$4(AbstractPollingEndpoint.java:348)
    at org.springframework.integration.util.ErrorHandlingTaskExecutor.lambda$execute$0(ErrorHandlingTaskExecutor.java:57)
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
    at org.springframework.integration.util.ErrorHandlingTaskExecutor.execute(ErrorHandlingTaskExecutor.java:55)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint.lambda$createPoller$5(AbstractPollingEndpoint.java:341)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:96)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
    at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: org.springframework.messaging.MessagingException: Failed to execute on session
    at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:461)
    at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer.synchronizeToLocalDirectory(AbstractInboundFileSynchronizer.java:341)
    ... 20 more
Caused by: java.lang.IllegalStateException: Failed to create session.
    at org.springframework.integration.smb.session.SmbSessionFactory.getSession(SmbSessionFactory.java:65)
    at org.springframework.integration.smb.session.SmbSessionFactory.getSession(SmbSessionFactory.java:39)
    at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:447)
    ... 21 more
Caused by: java.io.IOException: Unable to initialize share: smb://myDomain;myUser:myPassword@myHost:445/myShareAndDir
    at org.springframework.integration.smb.session.SmbShare.init(SmbShare.java:113)
    at org.springframework.integration.smb.session.SmbSessionFactory.createSession(SmbSessionFactory.java:86)
    at org.springframework.integration.smb.session.SmbSessionFactory.getSession(SmbSessionFactory.java:62)
    ... 23 more
Caused by: jcifs.smb.SmbException: Failed to connect to server
    at jcifs.smb.SmbTreeConnection.connectWrapException(SmbTreeConnection.java:429)
    at jcifs.smb.SmbFile.ensureTreeConnected(SmbFile.java:573)
    at jcifs.smb.SmbFile.exists(SmbFile.java:873)
    at org.springframework.integration.smb.session.SmbShare.init(SmbShare.java:98)
    ... 25 more
Caused by: java.net.UnknownHostException: myHost
    at jcifs.netbios.NameServiceClientImpl.getAllByName(NameServiceClientImpl.java:1054)
    at jcifs.netbios.NameServiceClientImpl.getAllByName(NameServiceClientImpl.java:55)
    at jcifs.smb.SmbTransportPoolImpl.getSmbTransport(SmbTransportPoolImpl.java:173)
    at jcifs.smb.SmbTransportPoolImpl.getSmbTransport(SmbTransportPoolImpl.java:48)
    at jcifs.smb.SmbTreeConnection.connectHost(SmbTreeConnection.java:565)
    at jcifs.smb.SmbTreeConnection.connectHost(SmbTreeConnection.java:489)
    at jcifs.smb.SmbTreeConnection.connect(SmbTreeConnection.java:465)
    at jcifs.smb.SmbTreeConnection.connectWrapException(SmbTreeConnection.java:426)
    ... 28 more

2023-04-29T18:35:10.725+02:00 DEBUG 4300 --- [   scheduling-1] o.s.i.channel.PublishSubscribeChannel    : postSend (sent=true) on channel 'bean 'errorChannel'', message: ErrorMessage [payload=org.springframework.messaging.MessagingException: Problem occurred while synchronizing 'folder1' to local directory, headers={id=8a317e74-0c7e-129e-c7b2-e9a7a73a7be5, timestamp=1682786110723}]
2023-04-29T18:35:15.733+02:00  INFO 4300 --- [   scheduling-1] o.s.i.smb.session.SmbSessionFactory      : SMB share init: myHost:445/myShareAndDir

这是一个在www. example上创建的空Spring项目 www.example.com 添加任何依赖项。
我尝试配置有效的主机名和有效的域。所有这些都会导致同样的错误。除此之外,我还尝试更改ShareAndDir名称以匹配REMOTE_DIRECTORY。我删除并重新创建了磁盘上的目录。
Spring文档我已经看了很多遍了。
我很感激任何帮助。先谢谢你了。

vof42yt1

vof42yt11#

我对Springboot了解不多,但似乎这就是原因:

Caused by: java.net.UnknownHostException: myHost

您没有指定变量,而是指定了一个文本字符串,这是一个无效的地址。你应该使用类似"127.0.0.1"的东西。
这与其他配置相同,除了端口:

smbSession.setHost("myHost");
smbSession.setPort(445);
smbSession.setDomain("myDomain");
smbSession.setUsername("myUser");
smbSession.setPassword("myPassword");
smbSession.setShareAndDir("myShareAndDir");

相关问题