当前位置:网站首页>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)
}
边栏推荐
- 2022 East China Normal University postgraduate entrance examination machine test questions - detailed solution
- Redis command
- Architecture notes
- Crontab scheduled task
- [Code] occasionally take values, judge blanks, look up tables, verify, etc
- 10 000 volumes - Guide de l'investisseur en valeur [l'éducation d'un investisseur en valeur]
- 2021 year end summary
- 10000小时定律不会让你成为编程大师,但至少是个好的起点
- The dynamic analysis and calculation of expressions are really delicious for flee
- CentOS php7.3 installing redis extensions
猜你喜欢

Software testing learning - day 3

机器学习 | 简单但是能提升模型效果的特征标准化方法(RobustScaler、MinMaxScaler、StandardScaler 比较和解析)

JUC forkjoinpool branch merge framework - work theft

Personally design a highly concurrent seckill system

IC_EDA_ALL虚拟机(丰富版):questasim、vivado、vcs、verdi、dc、pt、spyglass、icc2、synplify、INCISIVE、IC617、MMSIM、工艺库

On the practice of performance optimization and stability guarantee

Create your own deep learning environment with CONDA
![[Fiddler problem] solve the problem about Fiddler's packet capturing. After the mobile network is configured with an agent, it cannot access the Internet](/img/9d/42dfef67246740f0dba0c6d8f1b625.jpg)
[Fiddler problem] solve the problem about Fiddler's packet capturing. After the mobile network is configured with an agent, it cannot access the Internet

EasyExcel

2022-06-23 VGMP-OSPF-域間安全策略-NAT策略(更新中)
随机推荐
Summary of remote connection of MySQL
Resttemplate configuration use
[LeetCode]404. Sum of left leaves
HMS core helps baby bus show high-quality children's digital content to global developers
Book recommendation~
Journal quotidien des questions (11)
2022-06-23 VGMP-OSPF-域间安全策略-NAT策略(更新中)
MySQL installation
Specified interval inversion in the linked list
Climb movie paradise 2021 hot
【无标题】
Interface learning
How can the server set up multiple interfaces and install IIS? Tiantian gives you the answer!
服务器如何设置多界面和装IIS呢?甜甜给你解答!
Unit test notes
CentOS php7.3 installing redis extensions
[Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones
[Fiddler problem] solve the problem about Fiddler's packet capturing. After the mobile network is configured with an agent, it cannot access the Internet
dataworks自定义函数开发环境搭建
php artisan