Important thing to say three times, there is Wood, Java to write DBF file to remote 2003 server, first time, second time, third time ... That's enough, Wang Er, there's no more, hurry up.
Demand
This morning, Wang ER (i) received a request:
Write the DBF Library of the market to the remote Windows Server2003!
Analysis
Before, I wrote a Java operation DBF file database, but how to write DBF to the remote server, this seems to need something.
So I went to ask the Niang ah, see if there is any package good jar package and so on, good to use.
Toss a key word, finally found a Java read and write Windows shared folder, thank Bo Master of good and hard intentions ah, so for Bo Master point a praise.
Take doctrine
first step, download Jcifs
Website: http://jcifs.samba.org/
Add the Jcifs-1.3.18.jar to the Project Jar package Library.
The second step, set the shared folder , of course, you need to set the user name password
How to get clients to access the WINDOWS2003 server to share files
The third step, demo, first explore the way through
Public Static void Main(string[] args) {Try{//define Dbfwriter instance to write DBF fileDbfwriter writer =NewDbfwriter (); Writer.setcharactersetname ("GB2312"); Dbffield fields[] =Newdbffield[1];//S1 HQZQDM Securities Code Char 6fields[0] =NewDbffield (); fields[0].setname ("S1"); fields[0].setdatatype (Dbffield.field_type_c); fields[0].setfieldlength (6); Writer.setfields (fields); object[] Obj1 =Newobject[1]; obj1[0] ="000001"; Writer.addrecord (OBJ1); String Remoteurl ="Smb://abc:[email protected]/dbf/pengbo.dbf"; Smbfile Smbfile =NewSmbfile (Remoteurl); Smbfile.connect ();//try to connectOutputStream fos =NewBufferedoutputstream (Smbfile.getoutputstream ()); Writer.write (FOS); Fos.close (); }Catch(Exception e) {Logger.error (E.getmessage ()); Logger.error (E.getmessage (), E); } }
Note Remoteurl
SMB: I don't know what that means, it's supposed to be a protocol, right? Wang Er, you're so smattering, aren't you? Well, then ask the mother.
The SMB (Server message Block) is the protocol name that can be used for WAP connections and communication between the client and the server.
Abc:123, you share the user name and password specified in the permissions
- 127.168.1.11, Nature is the remote IP.
- DBF, shared Folders
- PENGBO.DBF,DBF file name
- Six, there is nothing, if the above 5 points are not clear, then please direct messages to the King two, or, ask the Niang, beauty such as flowers, have asked to answer????
Run a bit.
Write this should be finished, I think it is, but the King two to say a little more, OK, I should be myself!
Repair
In fact, write to the above here, also really finished, but the king two days a pursuit of a perfect heart, this piece of code a little regret, smbFile.connect(); //尝试连接
especially spend time, and the market DBF library real-time refresh, so certainly not!
So what needs to be done is, you look at the following steps, I decided to enlarge the recruit:
The first step is to initialize the remote connection at the start of the project.
Write a Dbflistener class.
Private StaticLogger Logger = Logger.getlogger (Dbflistener.class);PrivateSmbfile Smbfile =NULL;PrivateFile File =NULL; Public void Start() {Try{if(COMMONSTANTS.REMOTE_FLAG_DBF) {//need to write DBF remotelyString Remoteurl ="smb://"+ COMMONSTANTS.USERNAME_DBF +":"+ COMMONSTANTS.PASSWORD_DBF +"@"+ commonstants.path_info_dbf; Smbfile =NewSmbfile (Remoteurl); Smbfile.connect (); Logger.info ("DBF remote connection started successfully!" "); }Else{file =NewFile (COMMONSTANTS.PATH_INFO_DBF); Logger.info ("DBF local file created successfully!" "); } }Catch(Exception e) {Logger.error (E.getmessage ()); Logger.error (E.getmessage (), E); } }//When the market changes, the market is written Public void writedbf(list<quotation> dbfquotations) {Executorservice pool = executors.newsinglethreadexecutor (); Pool.execute (NewThread (NewDbfthread (Smbfile, dbfquotations, file)); Pool.shutdown (); Logger.info ("DBF Write thread started ..."); }
When the project runs, try to connect to the remote DBF server
Logger.info ("Market server starts to connect to DBF service");intj =1; Dbflistener Dbflistener =NewDbflistener (); while(true) {Try{Dbflistener.start (); Break; }Catch(Exception e) {Logger.info ("After 5 seconds, try the first."+ (++J) +"Secondary connection to the DBF service, a reconnection reason occurred:"+ e.getmessage ());Try{Thread.Sleep ( the); }Catch(Interruptedexception E1) {Logger.error (E1.getmessage (), E1); }}} Dbfmanager.setdbflistener (Dbflistener);
second step, modular DBF operation
Write a DBF operation class Dbfoperation
Public Static void writedbfbyquotation(Smbfile smbfile, list<quotation> quotaionlist, file file) {Try{//define Dbfwriter instance to write DBF fileDbfwriter writer =NewDbfwriter (); Writer.setcharactersetname ("GB2312"); Dbffield fields[] =Newdbffield[ -];//S1 HQZQDM Securities Code Char 6fields[0] =NewDbffield (); fields[0].setname ("S1"); fields[0].setdatatype (Dbffield.field_type_c); fields[0].setfieldlength (6);...if(file!=NULL) {FileOutputStream Fos =NewFileOutputStream (file); Writer.write (FOS); Fos.close (); }if(Smbfile! =NULL) {OutputStream Fos =NewBufferedoutputstream (Smbfile.getoutputstream ()); Writer.write (FOS); Fos.close (); }
The third step, when the market comes, write it in
dbfListener.writeDBF(dbfQuotations); logger.info("写入DBF数据库结束");
This code can not be affixed to the whole, had to pick up the important post, right when given a reference. The main idea is that the system initializes the Smbfile object, then encapsulates the DBF write, more flexibly to the remote or local write, and, finally, the ability to initiate thread writes without affecting the performance of the current thread at the time of the market refresh. Wang ER programming ability is very general, although he always do not admit, no way, who let me is Wang er?!
Related articles
- Java Operation DBF File database
Quotations from Wang ER
Then life where not reptile, crawler please mark Http://blog.csdn.net/qing_gee
If you can heart without distractions, concentrate on doing their own thing, to do the best of their own, you can unconsciously surpass others, across the mediocre gap, stand out! -It's good to write, but it's not written by my Wang!
Copyright notice: This is the self-styled qing_gee of the kick-off workers, in writing a high-quality blog column. Let you have a happy reading time, is no longer a luxury dream!
Java writes DBF file to remote 2003 server