fetchimages.java

来自「URL programming. This example illustrate」· Java 代码 · 共 50 行

JAVA
50
字号
import java.net.*;
import java.io.*;

/* 
 * This example illustrates using a URL to access resources
 * on a secure site.
 *
 * If you are running inside a firewall, please also set the following
 * Java system properties to the appropriate value:
 *
 *   https.proxyHost = <secure proxy server hostname>
 *   https.proxyPort = <secure proxy server port>
 * 
 */

 class URLReader {
    public static void main(String[] args) throws Exception {
	System.setProperty("http.proxyHost", "193.226.5.55");
    System.setProperty("http.proxyPort", "3128");

	URL url = new URL("http://mercur.utcluj.ro/lab/test.jpg");
	URLConnection url_connection = url.openConnection();
	
	BufferedInputStream in = new BufferedInputStream(url.openStream());

	System.out.println("Locatie accesata...");

	int contor = url_connection.getContentLength();

	byte file_bytes[] = new byte[contor];
	
	
	System.out.println("Citesc "+contor+" bytes...");

	in.read(file_bytes, 0, contor);

	System.out.println("Date citite.");

	try{
		FileOutputStream iesire=new FileOutputStream ("downloaded.jpg");
		iesire.write (file_bytes, 0, contor);
	}
	catch(Exception e){
		System.out .println("Eror: "+e.toString ());
	}

	in.close();
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?