Java network function and programming

xiaoxiao2021-03-06  37

Xu Yingxia Shanghai University Calculation Center 25 #

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 and easy-to-understand 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. Perform the command line: "Javac file name" to compile the Java program. After compiling, generate the character code file that suffix .class. Finally, if it is Java Applitcation, execute the command line: "Java Biode Code 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 the command line: "AppletViewer HTML File Name" to run the Java Applet. Or open the HTML document with Netscape. 3. About the procedure of this article In order to make the program to be the most concise programming method, the programs in this paper generally use the simplest form, omitting the thread and other content. Therefore, the procedure of this article 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 also run the web service software such as WebStar for Win95 on a single machine, and modify the corresponding http: // www. Sh. .Edu.cn / ~ xyx / part of this article "http: // The local IP address "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 (MalformedurLexception E) {system.out.println (" can't open the url ");} image = getImage (imgur); previous format to generate URLs with" New URL (URL) " Object, and directly as a parameters of GetImage, the latter format first generates a URL object with "New 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 generated IMAG .CLASS and IMAG2.CLASS generated after compiling, 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. (For other different Java Applets in this article, the corresponding HTML document will modify the corresponding "code = imag.class".) Fourth, obtain the sound Java from the network to obtain sound files from the network and play the programming method of sound There are two categories, one is to use the Play (URL) and Play (URL, String) provided by Java to directly play the sound file on the network, and the other is obtained from the GetAudioclip (URL) or GetAudioclip (URL, String) first get from the network. The sound file and generate an object of the AUDIOCLIP type and then operates 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 four formats above are part of the URL object part - "New URL (URL)" 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 from the previously acquired and playing sound files to each other, to make 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/bark.au "; try {play (new url);} catch (exception e) {}}} ● program 5 format two import java.applet Import java.Awt. *; public class sound2 extends applet {audioclip loopclip; public void paint (graphics g) {string audur = "http://www.shu.edu.cn/ ~ XYX / Java / Animator / Audio / "; 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 / Bark.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 / Audio / ");} 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 supplied 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 (Docurs});}}} ● 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 ("can't get URL:") ("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 not 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 "/ n");} } catch (ioException e) {system.out.println ("Error In I / O:" E.getMessage ());}}} 7. Dynamic Using Network Resources On the basis of the examples of the previous introduction, you can dynamically Localized network resources. 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 of the following examples. For example, an example of reading http://www.shu.edu.cn/ -ax/doc/manhua .html file content is read in the above example, as shown below 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 () {ix (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 "/ n");}} catch (ooException e) {system.out.println ("Error I / O:" E.getMessage ());} TRY {DTHREAD .sleep (5000);} Catch (InterruptedException E) {} repaint ();}} 8. The Java applet can only be established with its host when browsing with Netscape, Java applet Connection, therefore, most of the program can only be stored on 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, there is no such limit on other HTML documents on the display network (such as programs 8, 9), and the reader can compile the program to any WWW server or FTP server can be 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, just open with Netscape's File / Open File menu, 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 "/ n");}} catch (ooException e) {system.out.println ("Error In I / O:" E.getMessage () );}}} After using the showfile2.java store, use Java SHOWFILE2.JAVA to compile "Java ShowFile2", you can print http://www.shu.edu.cn/~xyx/ Doc / manhua.html file content. 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.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-61083.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="61083" 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.027</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 = 'j5V1wwakmpQgZXZxFIstpXtWCFvS0XkGqJv2moKg5uCOAijagtsvMTlynQmkOm4NCPCgY1hlKbFfDoqmjN9LiQ_3D_3D'; 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>