楼主: Lisrelchen
1303 0

Downloading the Contents of a URL using Java [推广有奖]

  • 0关注
  • 62粉丝

VIP

已卖:4194份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50288 个
通用积分
83.6306
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

楼主
Lisrelchen 发表于 2015-6-5 10:57:44 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
Downloading the Contents of a URL

Example 5-1 shows how you can download the network resource referred to by a URL using the URL class. This class serves mainly to represent and parse URLs but also has several important methods for downloading URLs. The most high-level of these methods isgetContent( ), which downloads the content of a URL, parses it, and returns the parsed object. This method relies on special content handlers having been installed to perform the parsing. By default, the Java SDK has content handlers for plain text and for several common image formats. When you call the getContent( ) method of a URL object that refers to a plain text or GIF or JPEG image file, the method returns a String or Image object. More commonly, when getContent( ) doesn’t know how to handle the data type, it simply returns anInputStream so that you can read and parse the data yourself.

Example 5-1 doesn’t use the getContent( ) method. Instead, it calls openStream( ) to return an InputStream from which the contents of the URL can be downloaded. This InputStream is connected, through the network, to the remote resource named by the URL, but the URL class hides all the details of setting up this connection. (In fact, the connection is set up by a protocol handler class; the Java SDK has default handlers for the most common network protocols, including http:, ftp:, mailto: and file:.)

Example 5-1 is a simple standalone program that downloads the contents of a specified URL and saves it in a file or writes it to the console. You’ll note that most of this program looks like it belongs in Chapter 3. In fact, as we’ll see in this and other examples in this chapter, almost all networking involves the use of the stream-based I/O techniques we learned about in that chapter.

  1. Example 5-1. GetURL.java
  2. package je3.net;
  3. import java.io.*;
  4. import java.net.*;

  5. /**
  6. * This simple program uses the URL class and its openStream( ) method to
  7. * download the contents of a URL and copy them to a file or to the console.
  8. **/
  9. public class GetURL {
  10.     public static void main(String[  ] args) {
  11.         InputStream in = null;   
  12.         OutputStream out = null;
  13.         try {
  14.             // Check the arguments
  15.             if ((args.length != 1)&& (args.length != 2))
  16.                 throw new IllegalArgumentException("Wrong number of args");
  17.             
  18.             // Set up the streams
  19.             URL url = new URL(args[0]);   // Create the URL
  20.             in = url.openStream( );        // Open a stream to it
  21.             if (args.length == 2)         // Get an appropriate output stream
  22.                 out = new FileOutputStream(args[1]);
  23.             else out = System.out;
  24.             
  25.             // Now copy bytes from the URL to the output stream
  26.             byte[  ] buffer = new byte[4096];
  27.             int bytes_read;
  28.             while((bytes_read = in.read(buffer)) != -1)
  29.                 out.write(buffer, 0, bytes_read);
  30.         }
  31.         // On exceptions, print error message and usage message.
  32.         catch (Exception e) {
  33.             System.err.println(e);
  34.             System.err.println("Usage: java GetURL <URL> [<filename>]");
  35.         }
  36.         finally {  // Always close the streams, no matter what.
  37.             try { in.close( );  out.close( ); } catch (Exception e) {  }
  38.         }
  39.     }
  40. }
复制代码

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:downloading contents download loading Content important represent download default network

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2025-12-30 09:59