当前位置:网站首页>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)
}
边栏推荐
- Ruoyi interface permission verification
- Modify MySQL password
- instanceof
- Resthighlevelclient gets the mapping of an index
- 万卷书 - 价值投资者指南 [The Education of a Value Investor]
- DBNet:具有可微分二值化的实时场景文本检测
- Error c2017: illegal escape sequence
- Operation principle of lua on C: Foundation
- 2022-06-23 VGMP-OSPF-域间安全策略-NAT策略(更新中)
- error C2017: 非法的转义序列
猜你喜欢
Inno setup production and installation package
Jenkins
Sorting out the core ideas of the pyramid principle
In depth analysis of reentrantlock fair lock and unfair lock source code implementation
vmware虚拟机C盘扩容
Use the jvisualvm tool ----- tocmat to start JMX monitoring
【无标题】
10000小时定律不会让你成为编程大师,但至少是个好的起点
Selenium key knowledge explanation
Integration test practice (1) theoretical basis
随机推荐
MySQL transaction rollback, error points record
centos php7.3安装redis扩展
My 2020 summary "don't love the past, indulge in moving forward"
In depth analysis of reentrantlock fair lock and unfair lock source code implementation
Pytest -- write and manage test cases
Centos切换安装mysql5.7和mysql8.0
Mise en place d'un environnement de développement de fonctions personnalisées
UTC time, GMT time, CST time
Application scenarios of Catalan number
Ruoyi interface permission verification
CentOS switches and installs mysql5.7 and mysql8.0
Specified interval inversion in the linked list
万卷书 - 价值投资者指南 [The Education of a Value Investor]
利用C#实现Pdf转图片
Climb movie paradise 2021 hot
Error c2017: illegal escape sequence
[Code] if (list! = null & list. Size() > 0) optimization, set empty judgment implementation method
C2338 Cannot format an argument. To make type T formattable provide a formatter<T> specialization:
熊市里的大机构压力倍增,灰度、Tether、微策略等巨鲸会不会成为'巨雷'?
10000小時定律不會讓你成為編程大師,但至少是個好的起點