当前位置:网站首页>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)
}
边栏推荐
- Upgrade CentOS php7.2.24 to php7.3
- Redis command
- 卡特兰数(Catalan)的应用场景
- 10000小时定律不会让你成为编程大师,但至少是个好的起点
- JMeter JSON extractor extracts two parameters at the same time
- [LeetCode]404. Sum of left leaves
- POI excel percentage
- 2022-06-23 VGMP-OSPF-域間安全策略-NAT策略(更新中)
- Resttemplate configuration use
- Specified interval inversion in the linked list
猜你喜欢

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

Application scenarios of Catalan number

Flask Foundation

Arctic code vault contributor

Reading notes of "learn to ask questions"

2022 East China Normal University postgraduate entrance examination machine test questions - detailed solution

JMeter JSON extractor extracts two parameters at the same time

JMeter test result output

Jenkins

On the practice of performance optimization and stability guarantee
随机推荐
2022 - 06 - 23 vgmp - OSPF - Inter - Domain Security Policy - nat Policy (Update)
Ruoyi interface permission verification
How to plan well?
The education of a value investor
Understand software testing
Upgrade CentOS php7.2.24 to php7.3
Software testing learning - day 3
MySQL installation
[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*
2022 East China Normal University postgraduate entrance examination machine test questions - detailed solution
These two mosquito repellent ingredients are harmful to babies. Families with babies should pay attention to choosing mosquito repellent products
Pytest attempts to execute the test case without skipping, but the case shows that it is all skipped
10 000 volumes - Guide de l'investisseur en valeur [l'éducation d'un investisseur en valeur]
Climb movie paradise 2021 hot
php artisan
Mise en place d'un environnement de développement de fonctions personnalisées
File links cannot be opened or downloaded in Google browser
Strategy mode
Dbnet: real time scene text detection with differentiable binarization
Software testing learning - the next day