Shared file reading and writing based on SMB Protocol blog category: Java

Source: Internet
Author: User
Tags get ip file transfer protocol

Read and write of shared files based on SMB protocol Blog Category:
    • Java
One, the SMB protocol
The SMB protocol is based on Tcp-netbios, and the general port usage is 139,445.
The server Information block (SMB) protocol is an IBM protocol that is used to share files, printers, serial ports, and so on between computers. The SMB protocol can be used on the TCP/IP protocol of the Internet or on other network protocols such as IPX and NetBEUI.
SMB a client/server, request/response protocol. Through the SMB protocol, client applications can read, write files on the server, and make service requests to server programs in various network environments. In addition, through the SMB protocol, applications can access remote server-side files, as well as resources such as printers, mail slots (mailslot), Named pipes (named pipe), and so on.
In a TCP/IP environment, the client connects to the server through NetBIOS over TCP/IP (or netbeui/tcp or spx/ipx). Once the connection succeeds, the client can send the SMB command to the server so that the client can access the shared directory, open the file, read and write the file, and everything that can be done on the file system.
Starting with Windows 95, the Microsoft Windows operating system (operating system) includes both client and server SMB protocol support. Microsoft provides an open source version of SMB for the Internet, the Universal Internet File System (CIFS). CIFS has greater flexibility than existing Internet applications such as File Transfer Protocol (FTP). For UNIX systems, you can use a shared software called Samba.
II. CIFS (Common Internet File System) protocol
CIFS is a newly proposed protocol that enables programs to access files on a remote internet computer and require services for this computer. CIFS uses client/server mode. The client program requests a server program on the server to service it. The server obtains the request and returns a response. CIFS is a public or open SMB protocol version and is used by Microsoft. The SMB protocol is now a protocol for server file access and printing on a local area network. Like the SMB protocol, CIFS runs at the top, rather than at the bottom, as the TCP/IP protocol does. CIFS can be seen as an implementation of application protocols such as file transfer protocols and hypertext transfer protocols.
Third, NetBIOS protocol
Netbios (network basic input/output system) was originally developed by Ibm,sytek as an API to enable user software to use local area network resources. Since birth, NetBIOS has become the foundation of many other Web applications. In strict sense, Netbios is the interface standard for accessing network services.
Netbios was originally designed as the network controller for the IBM LAN and is the software layer used by specific hardware to connect to the network operating system. NetBIOS is extended to allow programs to manipulate the IBM Token Ring structure using the NetBIOS interface. Netbios has been recognized as an industry standard, usually referred to netbios-compatible LANs.
It provides a set of methods for network programs to communicate and transmit data to each other. Basically, Netbios allows programs and network sessions. Its purpose is to separate the program and any type of hardware attributes. It also allows software developers to avoid the following burdens: Developing network bug fixes, low-level information addressing, and routing. With the NetBIOS interface, you can do a lot of work for software developers.
NetBIOS standardizes the interface between program and LAN operating capabilities. They can refine the program to which layer of the OSI model is written, allowing the program to be ported to other networks. In a NetBIOS LAN environment, the computer is known by the system by name. Each computer in the network has a permanent name that is compiled by different methods. These names will be discussed further below.
By using NetBIOS datagram or broadcast mode, the PC Setup session on the NetBIOS LAN is contacted with each other. Sessions allow more information to be transmitted, detect errors, and correct. Communication is on a one-to-one basis. Datagrams or broadcasts allow one computer to communicate with multiple other computers at the same time, but the information is limited in size. There are no detection errors and corrections using datagrams or broadcasts. However, datagram communication can not have to establish a session.
In this environment, all communication is submitted to NetBIOS in a format known as a "network control block". The allocation of these blocks in memory depends on the user program. These "network control blocks" are assigned to the domain and are reserved for input/output, respectively.
In today's environment, NetBIOS is the use of very common protocols. Ethernet, Token Ring, and IBM PC network all support NetBIOS. In its original version, it was used only as an interface for programs and network adapters. Since then, the Transfer class feature has been added to NetBIOS to make it more functional.
In NetBIOS, both connection-oriented (TCP) and non-connection (UDP) traffic are supported. It supports broadcast and multicast, supporting three separate services: naming, session, datagram.
Iv. accessing shared files on a local area network using the SMB protocol
1. Use jcifsThe jar package: jcifs-1.3.14.jar,:http:// jcifs. samba.org/
2, Local address: 10.130.14.37, local area network address: 10.130.14.111, its share file: share (set read, modify, Full Control permissions)
3. Java code
Package My.test;import Java.io.ioexception;importjcifs. SMB. Smbauthexception;importjcifs. SMB. Smbexception;importjcifs. SMB. Smbfileinputstream;importjcifs. SMB. Smbfile;importjcifs. SMB. Smbfileoutputstream;public class readsharefile{/***jcifsThe development method is similar to Java's file operation function, its resource URL location: smb://{user}:{password}@{host}/{path},* SMB is the protocol name, user and password are the login name and password of the shared file machine respectively, @ The host name or IP address of the resource to be accessed is followed. Finally, the shared folder name and shared resource name for the resource. * For example Smb://administrator:[email protected]/test/response.txt. * * In Java programs, use the following method to obtain a remote shared file (set permissions for shared files-read, modify, Full Control) handle: Smbfile file = Newsmbfile ("Smb://guest:[email protected]/ Share/a.txt "); * The handle here is not limited to remote shared files, but may also be shared folders. * The Isfile () method and Isdirectory () are used to determine the true properties of the resource corresponding to this handle. If it is a shared folder *, the list of resources within it will be obtained by calling its listing () method. * The list method supports filter mechanism, there are two kinds of filters to use, one is Smbfilefilter, the other is smbfilenamefilter*, both injcifsAs an interface, you can derive a personalized filter according to your needs, and implement the Accept method in the interface to meet the needs of different business. * * Smbfileinputstream is an input stream of an SMB file, and its function is to open a smbfile:smbfileinputstream* in = new Smbfileinputstream (file) as a stream; Smbfileinputstream provides the Read method, which allows you to read the entire contents of a remote file from this stream. * * @param args* @throws IOException */@SuppressWarnings ("static-Accesspublic static void Main (string[] args) {try {//LAN shared file, read file Smbfile smbfile = new Smbfile ("Smb://admini    Strator:[email protected]/share/aa.txt "); by Smbfile.isdirectory (); Isfile () can determine whether Smbfile is a file or a folder int length = Smbfile.getcontentlength ();//Get File size byte buff    er[] = new Byte[length]; Smbfileinputstream in = new Smbfileinputstream (smbfile);     Build the SMB file input stream while ((In.read (buffer))! =-1) {System.out.write (buffer);    System.out.println ("\ n" +buffer.length);   } in.close ();     Smbfile.delete ();    LAN shared file, write file Smbfile smbfileout = new Smbfile ("Smb://administrator:[email protected]/share/bb.txt");    if (!smbfileout.exists ()) smbfileout.createnewfile ();    Smbfileoutputstream out = new Smbfileoutputstream (smbfileout);    Out.write ("Abcdefw". GetBytes ());    Out.close ();   Smbfileout.delete ();    } catch (Smbauthexception e) {smbexception SMB = new smbexception (1,false); System.out.println (E.getntstatus () ==smb. Nt_statuS_ ACCESS_DENIED); for (int i=0;i<smb. nt_status_codes.length;i++) {System.out.println (SMB.    Nt_status_codes[i]);   } e.printstacktrace ();   } catch (IOException e) {e.printstacktrace (); }}}4, related error (1) share only Read permissionjcifs. SMB. Smbauthexception:Access  is denied. atjcifs. SMB. Smbtransport.checkstatus (smbtransport.java:528) atjcifs. SMB. Smbtransport.send (smbtransport.java:645) atjcifs. SMB. Smbsession.send (smbsession.java:244) atjcifs. SMB. Smbtree.send (smbtree.java:119) atjcifs. SMB. Smbfile.send (smbfile.java:770) atjcifs. SMB. Smbfile.delete (smbfile.java:2410) atjcifs. SMB. Smbfile.delete (smbfile.java:2354) at My.test.ReadShareFile.main (readsharefile.java:50)


If forwarding through the router requires specifying a port on the router, 139,445.

jcifsUnable to access XP shared directory issue jcifs. SMB. Smbauthexception
jcifs. SMB.         Smbauthexception:logon failure:unknown user name or bad password. Atjcifs. SMB. Smbsession.sessionsetup (smbsession.java:273) atjcifs. SMB. Smbsession.send (smbsession.java:225) atjcifs. SMB. Smbtree.treeconnect (smbtree.java:147) atjcifs. SMB. Smbfile.connect (smbfile.java:791) atjcifs. SMB. Smbfile.connect0 (smbfile.java:761) atjcifs. SMB. Smbfile.open0 (smbfile.java:816) atjcifs. SMB. Smbfile.open (smbfile.java:845) atjcifs. SMB. Smbfileinputstream.<init> (smbfileinputstream.java:69) atjcifs. SMB. Smbfileinputstream.<init> (smbfileinputstream.java:62) atjcifs. SMB. Smbfileinputstream.<init> (smbfileinputstream.java:49) at Cn.com.egova.dti.dao.AdjunctManager.main ( adjunctmanager.java:188)


WORKAROUND: Tools--Folder Options--View--Use simple file sharing, remove the check box


Get IP from domain name and determine if it is an IP address
   Java.net.inetaddress[] x= java.net.InetAddress.getAllByName ("www.aaa.com");   String IP = "";   if (x.length>0) {   IP = x[0].tostring (). Split ("/") [1];   }    /**       * IP address regular expression (?:(? : 25[0-5]|2[0-4]\\d| [01]?\\d?\\d] \ \.) {3} (?: 25[0-5]|2[0-4]\\d| [01]?\\d?\\d)]       *      /String IPADDRESS = "(?:(? : 25[0-5]|2[0-4]\\d| [01]?\\d?\\d] \ \.) {3} (?: 25[0-5]|2[0-4]\\d| [01]?\\d?\\d)] ";        Pattern pattern = pattern.compile (IPADDRESS);        Matcher Matcher = Pattern.matcher (x.tostring ());        Boolean isIp = Matcher.matches ();



Create a folder
try {if (!smbtempfile.isdirectory ()) Smbtempfile.mkdirs ();} catch (Smbexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}

Shared file reading and writing based on SMB Protocol blog category: Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.