smbj
smbj copied to clipboard
java.net.UnknownHostException for smb server ip
I used jcifs.smb for upload data from android to Windows folder by SMB1.0, but now needs to switch to SMB2.0 with smbj package
I face an error in connection
SMBClient client = new SMBClient();
try (Connection connection = client.connect(smbUrl))
SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See https://www.slf4j.org/codes.html#noProviders for further details. java.net.UnknownHostException: smb://xxx.xxx.x.xxx:445/ at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:208) at java.net.Socket.connect(Socket.java:646) at com.hierynomus.protocol.commons.socket.ProxySocketFactory.createSocket(ProxySocketFactory.java:87) at com.hierynomus.protocol.commons.socket.ProxySocketFactory.createSocket(ProxySocketFactory.java:63) at com.hierynomus.smbj.transport.tcp.direct.DirectTcpTransport.connect(DirectTcpTransport.java:95) at com.hierynomus.smbj.connection.Connection.connect(Connection.java:139) at com.hierynomus.smbj.SMBClient.getEstablishedOrConnect(SMBClient.java:96) at com.hierynomus.smbj.SMBClient.connect(SMBClient.java:71)
I try the smbURL format as follow, it works when I used jcifs.smb with SMB1 protocol, but it does not work in connection with SMB2.0 with smbj package smb://xxx.xxx.x.xxx:445/
I have also tested in PowerShell whether SMB 2 protocol which shows true
PS C:\Users\user1> Get-SmbServerConfiguration | Select EnableSMB2Protocol
EnableSMB2Protocol
------------------
True
Can anyone advise how smbURL needs to modified, or any other changes(disable SMB1Protocol) I need to made in order to establish the connection successfully? Thanks.
The following are the further codes in which I try to upload file to SMB-shared folder
SMBClient client = new SMBClient();
try (Connection connection = client.connect(smbUrl)) {
AuthenticationContext authContext = new AuthenticationContext(username, password.toCharArray(), "");
Session session = connection.authenticate(authContext);
DiskShare share = (DiskShare) session.connectShare("shared-folder");
try (InputStream inputStream = new FileInputStream(localFilePath)) {
File file = share.openFile(remoteFilePath,
EnumSet.of(AccessMask.GENERIC_WRITE), null,
SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_CREATE,
EnumSet.of(SMB2CreateOptions.FILE_RANDOM_ACCESS));
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
file.write(buffer, 0);
}
file.close();
Change client.connect(smbUrl)
to client.connect(servername, port)
The default port is 445, so if that it the port you can just use client.connect(servername)
But the parameter you pass to connect needs to be a valid hostname or IP-adress, it can not be a full smburl.
Change
client.connect(smbUrl)
toclient.connect(servername, port)
The default port is 445, so if that it the port you can just useclient.connect(servername)
But the parameter you pass to connect needs to be a valid hostname or IP-adress, it can not be a full smburl.
Yes, I miss the alternative implementation of connect function with separated port, this works for me, thanks 👍