当前位置:网站首页>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);
}
}
边栏推荐
- Spark kernel (execution principle) environment preparation /spark job submission process
- 【youcans 的 OpenCV 例程200篇】201. 图像的颜色空间转换
- Spark optimization - data skew solution
- Simulink code generation: table lookup module and its code
- [azure data platform] ETL tool (2) -- azure data factory "copy data" tool (cloud copy)
- How much can I get after the insurance period of annuity insurance products expires?
- Doris data backup and recovery
- Workflow of driver of spark kernel (stage division, task division, task scheduling)
- Solution of Kitti data set unable to download
- [synchronization function] version 2.0.16-19 has the update of synchronization function repair, but the problem has not been solved
猜你喜欢

Spark Optimization -- differences and policy selection of RDD cache (cache, persist, checkpoint)

Azure SQL db/dw series (9) -- re understanding the query store (2) -- working principle
![[azure data platform] ETL tool (2) -- azure data factory](/img/31/3561a3c3f24bce098330218a1d9ded.jpg)
[azure data platform] ETL tool (2) -- azure data factory "copy data" tool (cloud copy)

Graph data modeling tool

A data modeling optimization solution for graph data super nodes

Spark core concepts: Master, worker, driver program, executor, RDDS

Neil eifrem, CEO of neo4j, interprets the chart data platform and leads the development of database in the next decade

MySQL transaction isolation level experiment

【测试开发】博客系统——Loadrunner性能测试(发布博客功能 基准测试)

Summary of rust language practice
随机推荐
CXGRID keeps the original display position after refreshing the data
Common command records of redis client
[azure data platform] ETL tool (9) -- ADF performance optimization case sharing (1)
Transaction processing in PDO
Doris creates OLAP, mysql, and broker tables
LVS四层负载均衡集群(6)LVS工作模式
MMAP usage in golang
ONNX+TensorRT+YoloV5:基于trt+onnx得yolov5部署1
Implode and explode in golang
The most complete ongdb and neo4j resource portal in history
MySQL learning summary 6: data type, integer, floating point number, fixed-point number, text string, binary string
Nuggets new oil: financial knowledge map data modeling and actual sharing
[azure data platform] ETL tool (4) - azure data factory debug pipeline
Filters in PHP
How to Draw Useful Technical Architecture Diagrams
DDL operation table
LeetCode185. All employees with the top three highest wages in the Department (MySQL)
Application scenarios of large arrows in Scala
Get to know druid IO real time OLAP data analysis storage system
Redis memory optimization and distributed locking