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

问题描述 投票:0回答:1

我有一个新的 Spring 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 Documentation。我添加了一个全局字符串“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 级别(application.properties:

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


这是一个在 start.spring.io 上创建的空 Spring 项目,没有添加任何依赖项。

我尝试配置一个有效的主机名和一个有效的域。所有导致相同的错误。除此之外,我还尝试更改

ShareAndDir
名称以匹配
REMOTE_DIRECTORY
。我删除并重新创建了磁盘上的目录。

我多次阅读 Spring 文档

感谢任何帮助。提前致谢。

java spring spring-boot smb
1个回答
0
投票

我不太了解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");
© www.soinside.com 2019 - 2024. All rights reserved.