Java network function and programming

zhaozj2021-02-11  222

Java network function and programming

Summary: Java language is the most popular programming language on the Internet. This article is aimed at Java's network function, which has made a preliminary introduction from the programming method such as image, sound, HTML document and text files from the Internet. The method of resources on the network has been introduced. A large number of concise understandable examples are provided. Key words: Java; Internet; Network Java language is an emerging programming language on the Internet. There are many articles for Java's characteristics and basic programming methods. However, the majority of Java enthusiasts more want to learn more about Java's deeper programming methods, this article, this article, a preliminary introduction to Java's network function and its programming method. In order to facilitate the first time to contact Java readers, this article briefly introduces some of Java programming. I. Introduction to Java Programming 1. Programming Environment: For most readers, the following configuration is a more economical option: operating system Win95 compile software JDK1.01 browsing software Netscape 2.0 or more (32-bit) 2. Programming method: first Enter the Java program with the text editor such as Edit, Notepad, etc., with .java as the file name suffix store. Run the command line again: Javac file name to compile the Java program. After compiling, generate the character code file that suffix .class. Finally, if you are Java Applitcation, execute the command line: Java byte file name to run the Java program. If it is a java applet, enter the HTML document of the Java Applet with the text editor, with .htm as the file name subsequent replication. Perform a command line: AppletViewer HTML file name to run Java Applet. Or open the HTML document with Netscape. 3. Description of the procedures in this article In order to make the program to be the most concise programming method, the procedures in this paper generally use the simplest form, omitting the thread and other content. Therefore, this document is not a good program, but it is easier to understand the beginner. After all the programs in this article, the generated byte code file and the corresponding HTML document are uploaded to http://www.shu.edu.cn/~xyx/test/jvnet, can run correctly. The reader connected to the Internet can open the address with the browser to view the running effect. Readers connecting the Internet can also enter and compile the program in the local hard drive. Open the HTML document with Netscape's File / Open File menu, experience the programming method and view the running effect. If the reader wants to put Java Applet on your host or other FTP server, use the HTTP protocol or FTP protocol in Netscape, for security restrictions, should be modified as follows: If the reader has an account on a WWW host You can do your personal homepage (usually in the user's root directory to create a WWW or public_html directory, HOMEPAGE's address is http: // Hostname / ~ Personal account), you can put the corresponding http://www.shu.edu in this article .cn / ~ xyx / part is modified to reader's own web node address, and then load the generated byte code file and the corresponding HTML document to your own node. If the reader's computer is connected to the Internet, you can also find an FTP node that can be uploaded, such as: ftp: //ftp.shnet.edu.cn/incoming, the corresponding http: //www.shu in this article. The edu.cn/~xyx/ section is changed to the address of the FTP node, and the generated byte code file and the corresponding HTML document are uploaded to the node to view the running effect.

If the reader's computer is not connected, you can run the web service software on a single machine such as WebStar for Win95, and modify the corresponding http: // www. Shu.edu.cn/~xyx/ part of this article to http: // Local The IP address forms to simulate network programming. Second, Java Network Functions and General Steps Java programs for access to networks can obtain resources such as images, sounds, HTML documents, and texts such as network nodes, and can process the obtained resources. For example, a Java program can read the latest data provided by a node every other time, and displayed in the form of a chart. On programming processing, it is generally manually made into a URL type object, and then obtains the resources represented by the object with the corresponding method (Method) in Java. Several examples of Java network functions are described below, and there are several different programming methods. Third, obtain image Java Applet from the network can directly obtain an image directly from the network and display it. In order to understand the programming method and from the local display image, we don't consider the network function first, let's take a simple image display: ● Program 1 Import java.applet. *; Import java.awt. *; public class imag0 extends applet {image image; public void init () {image = getImage (GetDocumentBase (), "Test.gif");} public void Paint (Graphics G) {g.drawImage (image, 0, 0 ,}} This is an example of the simplest acquisition and display image. In this example, first call the image Test.gif from the HTML document in the location (GetDocumentBase (), Image File Name). And thus generate an object image of an Image type, then display the image on the screen with DrawImage (Image, 0, 0, 0, THIS). If you want to get images from the other nodes on the network, the key is to create an object corresponding to other nodes on the network. Once the object of the iMage type is obtained, you can make any possible image operations. Java provides the following ways to create images corresponding to other nodes: GetImage (new url) can have two types: string url = "node URL"; image Image; try; (NEW URL (URL));} catCH (Exception E) {system.out.println ("CAN`t Open THE URL");} or url imgur = null; image image; try {imgur = new url ("knot Point URL ");} Catch (Malformedurlexce E) {system.out.println (" CAN`t Open THE URL ");} Image = GetImage (IMGUR); the previous format generates a URL object with the New URL (URL), And directly as the parameters of GetImage, the latter format first generates a URL object with the New URL (URL), and then passed to GetImage. The two formats are essentially the same. In both formats, parts generating the URL object are included in the try {obtaining URL object} catch (MalforMedurLexception E) {Error Tip}.

For example, you want to call the image of http://www.shu.edu.cn/~xyx/img/shnet.jpg, the first format is complete, as follows: ● Program 2 Import java.applet. *; Import Java .NET. *; Import java.awt. *; public class imag extends applet {image image; public void = "http://www.shu.edu.cn/~xyx/img/shnet. JPG "; try {image = getImage (new url (url));} Catch (Exception E) {}} PUBLIC VOID PAINT (GRAPHICS G) {g.drawImage (image, 0, 0, this);}} The complete procedures are the following procedures: ● Programs 3 Import java.applet. *; Import java.net. *; Import java.awt. *; Public class imag2 extends applet {image image; url imgur = null; public void init () {Try {imgur = new url ("http://www.shu.edu.cn/ ~xyx/iMG/SHNET.JPG");} catch (mALFORMEDURLEXCEPTION E) {system.out.println ("CAN`t Open ");} image = getImage (imgur);} public void Paint (graphics g) {g.drawImage (image, 0, 0, this);}} The two programs are respectively imag.java and IMAG2, respectively. Java store, execute javac imag.java and javac imag2.java, will get the built-generated iMag.class and imag2.class, and finally create the HTML document that calls these two Java Applets, such as the HTML document corresponding to iMag.class can be as follows : example </ title> </ head> < Center> <applet code = imag.class width = 550 height = 250> </ applet> </ html> Put the HTML document into the Test.html file, open with Netscape, if your computer is connected to the Internet, you can See the image obtained from the network from the web displayed by the Java Applet. (For the other different Java Applets, the corresponding HTML document can be modified to modify the corresponding code = imag.class.) Fourth, get the sound java from the network to obtain sound files from the network and play the sound programming method. Class, one is to use Java's Play (URL) and Play (URL, String) to directly play the sound file on the network, and the other is obtained from the GetAudioclip (URL) or GetAudioclip (URL, String) to get a sound file from the network. And generate an object of the AUDIOCLIP type and then operate the object.</p> <p>The usage format of the former is: string audur = "node URL"; try {play (new url);} catch (exception e) {} or string audur = "Nonpoint URL"; try {Play (New URL (AUDUR), sound file name);} Catch (Exception E) {} The format used by the latter is: string audur = "node URL"; audioclip loopclip; try {loopclip = getaudioClip (new url);} Catch (Exception E) {System.out.Println ("CAN`t Open THE URL");} or string audur = "Node URL"; audioclip loopclip; try {loopclip = getaudioClip (new url), sound file Name);} catch (Exception E) {system.out.println ("CAN`t Open THE URL");} The above four formats are directly part of the generated URL object part --new URL (URL) as Play or GetAudioClip parameters;</p> <p>As the format of the first Play (URL), the following programming format can also be employed: url audur = null; try {audur = new url ("node URL");} catch (exception e) {system.out. PRINTLN ("CAN`t Open THE URL");} Play (Audur); following four formats of the four kinds of formats from the network and play the sound files to each other, to make the reference: ● Program 4 The format of an import java.applet *; import java.awt *; import java.net *; public class sound1 extends Applet {AudioClip loopClip; public void paint (Graphics g) {String Audur = "http:.. // www. Shu.edu.cn/~xyx/java/animator/audio/ba rk.au "; try {play (new url);} catch (Exception E) {}}} ● Procedure 5 Formats iMport Java. Applet. *; import java.awt. *; import java.net. *; public class sound2 extends applet {audioclip loopclip; public void Paint (graphics g) {string audur = "http://www.shu.edu.cn / ~ XYX / Java / Animator / Audi O / "; Try {Play (New URL (Audur)," Bark.au ");} Catch (Exception E) {}}} ● Procedure 6 Format three import java.applet. *; import java.awt. *; import java.net. *; public class sound extends applet {audioclip loopclip; public void init () {string audur = http://www.shu.edu.cn/~xyx/ Java / Animator / Audio / B Ark.au "; TRY {loopclip = getaudioclip (new url);} catch (exception e) {}} public void Paint (graphics g) {loopclip.loop ();}} ● program 7 format four import java.applet. *; import Java.awt. *; import java.net. *; public class sound0 extends applet {audioclip loopclip; URL AUUR; public void init () {auur = new url ("http://www.shu.edu.cn / ~ XYX / Java / Animator / AUD IO / ");} catch (Malformedurlexception E) {system.out.println (" can`t open the url ");} loopclip = getaudioclip (auur," bark.au ") Public void Paint (GRAPHICS G) {loopclip.loop ();}} 5. Displaying other HTML documents on the network Using Java's getAppletContext (). ShowDocument (URL) can display HTML documents of other nodes,</p> <p>There are two formats in the previous display of other nodes on the network, and there are two formats. Class showdoc expends applet {url docur = null; public void Paint (graphics g) {Try {docur = new url ("http://www.shu.edu.cn/~xyx/doc/manhua.html");} Catch (MalformedurLexception E) {system.out.println ("can`t open the url");} if (docur! = null) {getAppletContext (). showdocument (docur, "_ blank");}}} ● program 9 two format import java.applet *;. import java.awt *;. import java.net *;. public class showdoc2 extends Applet {URL docur = null;. public void paint (Graphics g) {try {getAppletContext () showDocument ( NEW URL ("http://www.shu.edu.cn/ ~ xyx / doc / manhua.html");} catch (Malformedurlexception E) {system.out.println ("Can`t Open the URL") }}}} 6. Read the contents of the file content on the network. The aforementioned network function is just an image, sound, and HTML document displayed or played on the network, and does not process its content. In fact, Java can also read the contents of the file on the network, and process their content. The steps to read the file content on the network can be as follows: 1. Create an object of a URL type such as: string url = "ftp://202.120.127.218/incoming/test/readtxt.html"; URL fileur; tryur = new URL (URL);} catch (Malformedurlexception E) {system.out.println ("CAN`t get URL:");} 2. Using the URL class OpenStream (), get the corresponding INPUTSTREAM class, such as: InputStream Filecon = fileur.openStream (); 3. Translate the InputStream object to the object of the DataInputStream class such as DataInputStream FileData = New DataInputStream (FileCon); Content, or use FileData.Readchar a character to read content. For the read content, various processes can be performed by the Java Applet, and the processing results are displayed in a variety of ways. The following example is an example of reading http://www.shu.edu.cn/~xyx/doc/manhua.html file content, for the sake of simplicity, this example only reads the contents of the file, and The text area is displayed.</p> <p>● Procedure 10 Import java.io. *; import java.net. *; Import java.awt. *; Import java.applet. *; Public class showfile extends applet {url fileur; Textarea Showarea = New Textarea ("please wait a While for get text ", 10, 70); public void init () {string url =" http://www.shu.edu.cn/~xyx/doc/manhua.html "; try {fileur = new URL url);} catch (MalformedURLException e) {System.out.println ( "Can`t get URL:");} add (showarea);} public void paint (Graphics g) {InputStream filecon = null; DataInputStream filedata = null ; String fileline; try {filecon = fileur.openStream (); filedata = new DataInputStream (filecon); while (! (fileline = filedata.readLine ()) = null) {showarea.appendText (fileline "");}} catch (IOEXCEPTION E) {system.out.println ("Error In I / O:" E.getMessage ());}}} Resources on the web. Its method is to prepare a thread and automatically read the latest content every other time. This article is no longer expanded in this article, and readers can refer to the following examples or directly. For example, an example of reading http://www.shu.edu.cn/~ww/doc/manhua.html file in the above example is as follows after adding the thread. This example updates the data every 5 seconds. If http://www.shu.edu.cn/~xyx/doc/manhua.html is stored in some change faster information such as the stock market, and there are programs to update their content at any time, then in the Web Add this Java Applet, allows the traffickeeper to get dynamic information. Further, the data can be processed in the program, and the processing result is displayed in a graphical manner. For example, the data of each time is plotted into a curve, and the traffickeeper can see the curve of dynamic changes.</p> <p>// program 11 import java.io. *; import java.net *;. Import java.awt *;. Import java.applet *;. Public class dynashow extends java.applet.Applet implements Runnable {Thread dthread; URL fileur; Textarea Showarea = New Textarea ("Wait for A While ...", 10, 70); public void init () {string url = "http://www.shu.edu.cn/~xyx/doc/manhua. HTML "; try {fileur = new URL (URL);} catch (mAlformedurlexception e) {system.out.println (" CAN`t get URL: ");} add (showarea);} public void start () {IF (DTHREAD == NULL) {DTHREAD = New Thread (this); dthread.start ();}} public void stop () {if (DTHREAD! = null) {DTHREAD.STOP (); DTHREAD = null;}} public void run () {InputStream filecon = null; DataInputStream filedata = null; String fileline; while (true) {try {filecon = fileur.openStream (); filedata = new DataInputStream (filecon); while ((fileline = filedata.readLine ( ))))! = null) {Showarea.AppendText (fileline ");}} catch (ooexception e) {system.out.println (" Error I / O: " E.getMessage ());} Try {DTHREAD . Leep (5000);} catch () repaint ();}}} eight, Java network capabilities limit, Java Applets can only connect to their hosts when browsing with Netscape Therefore, most of the procedure compiled in front can only be stored on the host corresponding to http://www.shu.edu.cn/~xyx. You need to change the node address in the program when you store other hosts. Otherwise, the browser will display the security error. However, the other HTML documents on the display does not have this limit (such as programs 8, 9), and the reader can compile the program to any WWW server or FTP server, can run. In addition, when the browser opens the HTML document that calls Java Applet from this site, it is not subject to this limit. Therefore, all programs in this article can be stored in this site, as long as the Netscape's File / Open File menu is opened, you can run correctly.</p> <p>For another Java program - Java Application, there is no such limit, such as program 10 reads the file content on the network, the corresponding Java Application can be programmed as follows: ● Procedure 11 Import Java.io. *; Import Java. . net *; import java.awt *; class showfile2 {public static void main (String args []) {InputStream filecon = null; DataInputStream filedata = null; String fileline; String url = "http:. //www.shu. Edu.cn/~xyx/doc/manhua.html "; URL fileur; tryur = new url (url); filecon = fileur.openStream (); filedata = new dataputstream (filecon); while ((fileline = filedata). Readline ())! = NULL) {System.out.println (FileLine ");}}}} Catch (IOException E) {System.out.Println (" Error In I / O: " E.getMessage ()); }}} After using the showfile2.java store, after compiling with javac showfile2.java, you can print http://www.shu.edu.cn/~xyx/doc/manhua on the screen with Java ShowFile2. The content of the HTML file. Nine, the method of creating a URL object In the previous example we uniformly created a URL object in the form of a New URL (URL string). In fact, Java provides four forms of creating URL objects: 1.New URL (URL string) This format is used in this format, such as: New url ("http://www.shu.edu.cn/ ~ XYX / DOC / MANHUA.HTML ") 2.NEW URL (protocol, host name, file name or path), such as string url =" http://www.shu.edu.cn/~xyx/img in program 2 /SHNET.JPG "; image = getImage (New URL (URL)); part can be changed to: image = getImage (New URL (" http "," www.shu.edu.cn "," / ~ xyx / img / ShNet.jpg ")); 3.NEW URL (protocol, host name, port number, file name or path) 1 such as NEW URL (" http "," www.shu.edu.cn ", 80," / ~ XYX / DOC / MANHUA.html ") 4.NEW URL (Benchmark URL, File Name, or Path) 10. Other ways to implement network functions have focused on the use of Java's URL classes to achieve sound, image, html from the network. Documentation and file data programming methods. Java's network function is very powerful, in addition to the above introduction, you can use the URLConnection class to achieve a wider range of network features, such as sending information to the CGI program on the WWW server; you can write customer software and services yourself by Socket and Serversocket classes. Software, and you can design a communication protocol.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-3678.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="3678" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.061</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'UKdh_2BUbb8rE7VkaSoZ9T9DUMfP74mKxG6mPE_2B_2FMBwFZ83nIzqP_2FHve595zlob8nLyCQpsvSG1NL1z3_2B_2F'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>