1. 程式人生 > >簡單的通訊(四)----URL類的使用

簡單的通訊(四)----URL類的使用

功能

URL程式設計,從網路上讀取資料;

程式碼

package com.demo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * 
 * @author Lynn URL五部分: http://192.168.0.128:8080/helloworld/index.jsp
 *
 */
public class Demo07 {
    public static void main(String[] args) throws MalformedURLException {
        // 資源的網路路徑;
        URL url = new URL("http://localhost:8080/Test/test.txt");
        System.out.println("協議:" + url.getProtocol());
        System.out.println("埠號:" + url.getPort());
        System.out.println("路徑:" + url.getPath());
        System.out.println("檔名:" + url.getFile());
        System.out.println("對映:" + url.getRef());
        System.out.println("查詢:" + url.getQuery());
        
    
        //讀入檔案的內容有兩種方法
        //方法一
        //獲取輸入流;
        InputStream is = null;
        FileOutputStream fos = null;
        byte[] data;
        int len;
        try {
            is = url.openStream();
            fos = new FileOutputStream(new File("test1.txt"));
            data = new byte[1024];
            while((len=is.read(data))!=-1) {
                fos.write(data, 0,len );
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(fos!=null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(is!=null) {
                try {
                    is.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
        
        //方法二
        //輸入流;
        InputStream is1 = null;
        //輸出流;
        OutputStream os = null;
        try {
            URLConnection urlConnection = url.openConnection();
            is1 = urlConnection.getInputStream();
            byte[] data1 = new byte[1024];
            int len1 = 0;
            while((len1=is1.read(data1))!=-1) {
                System.out.println(new String(data1,0,len1));
            }
            
//          urlConnection.setDoOutput(true);
//          os = urlConnection.getOutputStream();
//          
//          os.write("我是新增的內容".getBytes());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(os!=null) {
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            }
            if(is1!=null) {
                try {
                    is1.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
        
        
    }
}

執行結果

需要開啟伺服器,這裡使用的是tomcat伺服器。