当前位置:网站首页>Batch image Download & socket dialogue

Batch image Download & socket dialogue

2022-06-13 03:36:00 Mouth strong programmer


         * Character stream Reader- Read , Input
         * Character stream Writer- write in , Output
         * 
         *      File character stream :
         *         FileReader
         *         FileWriter
         * 
         *      Use steps :
         *         1. Build file object through File class
         *         2. take File Class object passed to character stream object
         *             FileReader fr = new FileReader(file);
         *         3. Read or write
         *              Read : Can read one character at a time  read
         *                  One character array can be read at a time read(buf)
         * 
         *              write in :
         *                 write(char)
         *                 write(char[] chs)
         *                 write(String str)
         * 
         *      Buffer flow
         *         BufferedReader
         *         BufferedWriter
         * 
         *         readLine();// Read a line
         *         newLine();// enjambment
         *         flush();// Refresh buffer
         * 
         * 
         * Converted flow
         *         InputStreamReader
         *         OutputStreamWriter
         * 
         * 
         * Network programming
         *          Realize data interaction between multiple devices
         * 
         *      Three elements :IP Address   Port number   Network communication transmission protocol
         *         IP:  InetAddress    
         * 
         *          Port number :
         *              browser ---80
         *             tomcat--8080
         *             sqlserver  1433
         *             oracle  1521
         * 
         *          Network communication transmission protocol (TCP|IP)
         *             TCP
         *             UDP
         *     
         *         URL Programming .
         

Use network stream to download multiple pictures

package com.zking.test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Demo_02 {

	public static void main(String[] args) throws Exception {
		// file:///D:/xb/default.html
		//  demand : Download the website page of STARBO 
		// 1. adopt URL Object to the specified resource 
		URL url = new URL("file:///D:/xb/default.html");
		// 2. adopt url Object to open and activate the network flow 
		InputStream is = url.openStream();
		// 3. Get the byte input stream   Convert to character stream   Then it is converted into a buffer stream 
		InputStreamReader isr = new InputStreamReader(is);
		//  buffer 
		BufferedReader br = new BufferedReader(isr);

		//  Define a string variable to splice the data read from each row 
		StringBuffer sb = new StringBuffer();

		//  Read one line of data at a time 
		String content = "";
		while (null != (content = br.readLine())) {
			// System.out.println(content);
			sb.append(content + "\n");
		}
		//  Closed flow 
		br.close();
		isr.close();
		is.close();

		// https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png
		// file:///D:/xb/images/banner.jpg
		// file:///D:/xb/images/logo.gif
		//  Source code <img src="images/logo.gif" />
		//  Can the image name on the page be found in the source code of the web page ?
		// file:///D:/xb/ + The name of the picture 
		
//		System.out.println(sb);
		// <img src="images/logo.gif" />
		// <img src="images/zp.gif" width="248" height="72" />
		//  Space  \\s
		//img In the label   src   width  height  alt  title  style
		String regex = "<img\\ssrc=\"([^\\\">]+)\"(\\swidth=\"\\w+\")?(\\sheight=\"\\w+\")?\\s/>";// Suppose you have written 
		//1. Instantiate by regular rules Pattern Regular pattern objects 
		Pattern compile = Pattern.compile(regex);
		//2. adopt compile Schema object creation Matcher object 
		Matcher matcher = compile.matcher(sb);
		//3. adopt find Methods and group Method traverses to find sb Everything in the string that conforms to the regular rules ( be-all img label )
		int count = 0;
		while(matcher.find()) {
			System.out.println(matcher.group(1));
			count++;
			//file:///D:/xb/images/btn_3.gif
			// Write a download method specifically    take matcher.group(1) The file name is passed as a parameter 
			downLoadImage("file:///D:/xb/"+matcher.group(1));
		}
		System.out.println(" The total number of pictures : "+count);
	}

	/**
	 *  How to download pictures 
	 */
	private static void downLoadImage(String filePath) throws Exception {
		//file:///D:/xb/images/btn_3.gif
		// Capture the picture name 
		int index = filePath.lastIndexOf("/");
		String fileName = "";
		if(-1!=index) {
			fileName = filePath.substring(index);
		}
		
		//1. establish URL Object will filePath Pass in the location resource 
		URL url = new URL(filePath);
		//2. Open and activate network flow 
		InputStream is = url.openStream();
		BufferedInputStream bis = new BufferedInputStream(is);
		//3. Output stream    Write locally ( download )
		File file = new File("D:\\"+fileName);
		FileOutputStream fos = new FileOutputStream(file);
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		
		// Read an array of bytes 
		byte[] bytes = new byte[1024];
		int len = 0;
		while(-1!=(len = bis.read(bytes))) {
			bos.write(bytes, 0, len);
			bos.flush();// Refresh 
		}
		bos.close();
		fos.close();
		bis.close();
		is.close();
		
	}
	
	
	
	
}

RUL Network chip programming server and client

package com.zking.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**
 *  Server side 
 * @author Administrator
 *
 */
public class ServerTest {
	public static void main(String[] args) throws Exception{
		System.out.println("--------------- The server ----------------");
		//1. Create server side    adopt ServerSocket
		ServerSocket ss = new ServerSocket(8989);
		System.out.println("QQ Server turned on .... Wait for the client to connect !!!");
		//2. Wait for a client to connect to the server 
		// adopt ss Object to call the method of the receiving client 
		Socket sk = ss.accept();// The way of blocking 
		System.out.println(" A client is online ......");
		
		// Receive messages from clients 
		InputStream is = sk.getInputStream();
		InputStreamReader isr = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(isr);
		
		// Read a line 
		String readLine = br.readLine();
		System.out.println(" client sb say : "+readLine);
		
		// reply 
		OutputStream os = sk.getOutputStream();
		OutputStreamWriter osw = new OutputStreamWriter(os);
		BufferedWriter bw = new BufferedWriter(osw);
		// Send a message 
		Scanner sc = new Scanner(System.in);
		System.out.println(" Please enter the content to send : ");
		String content = sc.next();
		bw.write(content);
		bw.newLine();
		bw.flush();
		
	}
}

client

package com.zking.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

/**
 *  client 
 * 
 * @author Administrator
 *
 */
public class ClientTest {
	public static void main(String[] args) throws Exception {
		System.out.println("------------------------ client --------------------");
		// 1. Use Socket Create client 
		Socket sk = new Socket(InetAddress.getByName("127.0.0.1"), 8989);
		System.out.println(" Client on , Successfully connected to the server ");

		//  a key : When the client connects to the server   Send a message   Flow through the network 
		OutputStream os = sk.getOutputStream();
		OutputStreamWriter osw = new OutputStreamWriter(os);
		BufferedWriter bw = new BufferedWriter(osw);
		//  Send a message 
		// String content = " Hello , The server ; I'm the client , I like drinking .";
		Scanner sc = new Scanner(System.in);
		System.out.println(" Please enter the content to send : ");
		String content = sc.next();
		bw.write(content);
		bw.newLine();
		bw.flush();

		//  Receive messages from clients 
		InputStream is = sk.getInputStream();
		InputStreamReader isr = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(isr);

		//  Read a line 
		String readLine = br.readLine();
		System.out.println(" The server sb say : " + readLine);
	}
}

原网站

版权声明
本文为[Mouth strong programmer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280529564279.html