当前位置:网站首页>Advanced API (local simulation download file)
Advanced API (local simulation download file)
2022-07-03 07:01:00 【Bugxiu_ fu】
Realize wireless chat between client and server
System.out.println("============= Server side ===============");
// Turn on the server
ServerSocket ss = new ServerSocket(7979);
// Waiting for client connections
Socket sk = ss.accept();
System.out.println(" Connected ");
// Input stream Read the message sent by the client
InputStream is = sk.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
// Resource path
String readLine = br.readLine();
// File
File file = new File(readLine);
FileInputStream fis = null;
InputStreamReader isr2 = null;
BufferedReader br2 = null;
// Write into the network
OutputStream os = sk.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
if (file.exists()) {// There is
// Read the contents of the specified resource in the server
fis = new FileInputStream(file);
isr2 = new InputStreamReader(fis);
br2 = new BufferedReader(isr2);
String str = "";
while (null != (str = br2.readLine())) {
bw.write(str);
bw.newLine();
bw.flush();
}
} else {// File resource path does not exist
bw.write(" File not found ");
bw.newLine();
bw.flush();
}
// Close all resources
bw.close();
osw.close();
os.close();
if (null != br2) {
br2.close();
}
if (null != isr2) {
isr2.close();
}
if (null != fis) {
fis.close();
}
br.close();
isr.close();
is.close();
ss.close();
}
System.out.println("================= client ===================");
Scanner sc = new Scanner(System.in);
// Open client
Socket sk = new Socket("127.0.0.1", 7979);
System.out.println("OK");
System.out.println(" Please enter the resource path to download : ");
String filePath = sc.next();
// Write the resource path into the network
OutputStream os = sk.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(filePath);
bw.newLine();
bw.flush();
// Receive the content sent by the server
InputStream inputStream = sk.getInputStream();
InputStreamReader isr = new InputStreamReader(inputStream);
BufferedReader br = new BufferedReader(isr);
// Get the name of the file
String fileName = filePath.substring(filePath.lastIndexOf("/"));
// After reading the content Write to local through file
FileOutputStream fos = new FileOutputStream("D:\\zking\\" + fileName);
BufferedWriter bw2 = new BufferedWriter(new OutputStreamWriter(fos));
String str = br.readLine();
if (" File not found ".equals(str)) {
System.out.println(" Resource does not exist ");
} else {
System.out.println(str);
bw2.write(str);
bw2.newLine();
bw2.flush();
while (null != (str = br.readLine())) {
// System.out.println(str);
bw2.write(str);
bw2.newLine();
bw2.flush();
// write(byte[],0,len)
}
边栏推荐
- In depth analysis of reentrantlock fair lock and unfair lock source code implementation
- Personally design a highly concurrent seckill system
- [attribute comparison] defer and async
- 【类和对象】深入浅出类和对象
- JMeter test result output
- Shim and Polyfill in [concept collection]
- Interface learning
- Error c2017: illegal escape sequence
- The dynamic analysis and calculation of expressions are really delicious for flee
- 服务器如何设置多界面和装IIS呢?甜甜给你解答!
猜你喜欢

JMeter JSON extractor extracts two parameters at the same time

golang操作redis:写入、读取hash类型数据

POI excel percentage

卡特兰数(Catalan)的应用场景

dataworks自定義函數開發環境搭建

Practical plug-ins in idea

Software testing learning - day one

Create your own deep learning environment with CONDA

【无标题】

Summary of UI module design and practical application of agent mode
随机推荐
php artisan
These two mosquito repellent ingredients are harmful to babies. Families with babies should pay attention to choosing mosquito repellent products
The 10000 hour rule won't make you a master programmer, but at least it's a good starting point
Reading notes of "learn to ask questions"
DNS forward query:
Daily question brushing record (11)
golang操作redis:写入、读取kv数据
PHP install composer
centos php7.2.24升级到php7.3
File links cannot be opened or downloaded in Google browser
Resthighlevelclient gets the mapping of an index
vmware虚拟机C盘扩容
Winter vacation work of software engineering practice
Mise en place d'un environnement de développement de fonctions personnalisées
What are the characteristics and functions of the scientific thinking mode of mechanical view and system view
Abstract learning
How to specify the execution order for multiple global exception handling classes
Personally design a highly concurrent seckill system
[untitled]
10000小时定律不会让你成为编程大师,但至少是个好的起点