当前位置:网站首页>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)
}
边栏推荐
- 爬虫代码基础教学
- 2021 year end summary
- 这两种驱蚊成份对宝宝有害,有宝宝的家庭,选购驱蚊产品要注意
- Operation principle of lua on C: Foundation
- Know flex box
- [untitled]
- Hands on redis master-slave replication, sentinel master-slave switching, cluster sharding
- 如何迁移或复制VMware虚拟机系统
- JUC forkjoinpool branch merge framework - work theft
- Troubleshooting of high CPU load but low CPU usage
猜你喜欢

La loi des 10 000 heures ne fait pas de vous un maître de programmation, mais au moins un bon point de départ

2022年华东师范大学计科考研复试机试题-详细题解

深度学习参数初始化(一)Xavier初始化 含代码

Application scenarios of Catalan number

机器学习 | 简单但是能提升模型效果的特征标准化方法(RobustScaler、MinMaxScaler、StandardScaler 比较和解析)
![[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

Machine learning | simple but feature standardization methods that can improve the effect of the model (comparison and analysis of robustscaler, minmaxscaler, standardscaler)

These two mosquito repellent ingredients are harmful to babies. Families with babies should pay attention to choosing mosquito repellent products

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

Jmeter+influxdb+grafana of performance tools to create visual real-time monitoring of pressure measurement -- problem record
随机推荐
MySQL mistakenly deleted the root account and failed to log in
[attribute comparison] defer and async
(翻译)异步编程:Async/Await在ASP.NET中的介绍
Error c2017: illegal escape sequence
DBNet:具有可微分二值化的实时场景文本检测
php安装composer
Setting up the development environment of dataworks custom function
Practical plug-ins in idea
Create your own deep learning environment with CONDA
mysql误删root账户导致无法登录
每日刷题记录 (十一)
Unit test notes
Class and object summary
[Code] occasionally take values, judge blanks, look up tables, verify, etc
The 10000 hour rule won't make you a master programmer, but at least it's a good starting point
How can the server set up multiple interfaces and install IIS? Tiantian gives you the answer!
php artisan
Know flex box
Golang operation redis: write and read kV data
Resttemplate configuration use