当前位置:网站首页>Local simulation download file
Local simulation download file
2022-06-13 03:36:00 【Mouth strong programmer】
Server side Turn on
package com.zking.test2;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
// Server side
public class FileServer {
public static void main(String[] args) throws Exception {
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();
}
}
Client upload and download path
package com.zking.test2;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
// client
public class FileClient {
public static void main(String[] args) throws Exception {
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)
}
}
}
}
Realize the simplified version of sending messages between the client and the server forms
package com.zking.test2;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
// Server side
public class QQServerChat extends JFrame {
ServerSocket ss = null;
Socket sk = null;
// On
JPanel jpa = new JPanel();
JButton jba = new JButton(" Start the service ");
// in
JTextArea jta = new JTextArea();
JScrollPane jsp = new JScrollPane(jta);
// Next
JPanel jpb = new JPanel();
JTextField jtfa = new JTextField(15);
JButton jbb = new JButton(" send out ");
JButton jbc = new JButton(" receive ");
public QQServerChat() {
this.setTitle("");
this.setSize(400, 500);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
// Form event
this.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent e) {
// When the form is closed , Take all the flow objects on the page without closing
//isr.close
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
});
jpa.add(jba);
jpb.add(jtfa);
jpb.add(jbb);
jpb.add(jbc);
this.getContentPane().add(jpa, "North");
this.getContentPane().add(jsp, "Center");
this.getContentPane().add(jpb, "South");
/**
* jba Start the click event of the server
*/
jba.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// ServerSocket Instantiation
try {
ss = new ServerSocket(8989);
// The prompt content is set to jta In text field
jta.append(" Server turned on ... Wait for the client to connect ; The port number of the server is : " + ss.getLocalPort() + "\n");
} catch (IOException e1) {
e1.printStackTrace();
}
// Solve the blocking problem through multithreading
// Please have a robot monitor to connect to the client of the server
new Thread() {
// Rewrite a method
public void run() {
while (true) {
try {
sk = ss.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
jta.append(" The client is online .....\n");
}
};
}.start();
//
// Ask another robot to automatically help us receive the messages sent by the client
new Thread() {
public void run() {
while (true) {
// Let me have a rest first
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(sk!=null) {
try {
InputStream is = sk.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String readLine = br.readLine();
jta.append(" Client said : "+readLine+"\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
}.start();
}
});
/**
* Send a message
*/
jbb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Get the contents of the input box
String content = jtfa.getText();
// adopt sk Socket calls network stream write in
if (sk != null) {// Successfully connected
try {
OutputStream os = sk.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
// Write
bw.write(content);
bw.newLine();
bw.flush();
// Set up the content
jta.append(" The server said : " + content + "\n");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
// receive
jbc.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (sk != null) {
try {
InputStream is = sk.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String content = br.readLine();
jta.append("\t client : " + content + "\n");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
this.setVisible(true);
}
public static void main(String[] args) {
new QQServerChat();
}
}
package com.zking.test2;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
// client
public class QQClientChat extends JFrame {
Socket sk = null;
// On
JPanel jpa = new JPanel();
JLabel jla = new JLabel("IP Address ");
JTextField jtfa = new JTextField(10);
JLabel jlb = new JLabel(" Port number ");
JTextField jtfb = new JTextField(10);
JButton jba = new JButton(" Connect to server ");
// in
JTextArea jta = new JTextArea();
JScrollPane jsp = new JScrollPane(jta);
// Next
JPanel jpb = new JPanel();
JTextField jtfc = new JTextField(15);
JButton jbb = new JButton(" send out ");
JButton jbc = new JButton(" receive ");
public QQClientChat() {
this.setTitle("");
this.setSize(500, 500);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
jtfa.setText("127.0.0.1");
jtfb.setText("8989");
jpa.add(jla);
jpa.add(jtfa);
jpa.add(jlb);
jpa.add(jtfb);
jpa.add(jba);
jpb.add(jtfc);
jpb.add(jbb);
jpb.add(jbc);
this.getContentPane().add(jpa, "North");
this.getContentPane().add(jsp, "Center");
this.getContentPane().add(jpb, "South");
/**
* jba Click event of the client connection server button
*
*/
jba.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String ip = jtfa.getText();
int port = Integer.valueOf(jtfb.getText());
try {
sk = new Socket(ip, port);
jta.append(" The client has successfully connected to the server \n");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Client reception
new Thread() {
public void run() {
while(true) {
if (sk != null) {
try {
InputStream is = sk.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String content = br.readLine();
jta.append("\t The server : " + content + "\n");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
};
}.start();
}
});
/**
* Send a message
*/
jbb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Get the contents of the input box
String content = jtfc.getText();
System.out.println(content);
// adopt sk Socket calls network stream write in
if (sk != null) {// Successfully connected
try {
OutputStream os = sk.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
// Write
bw.write(content);
bw.newLine();
bw.flush();
// Set up the content
jta.append(" Client said : " + content + "\n");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
// receive
jbc.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (sk != null) {
try {
InputStream is = sk.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String content = br.readLine();
jta.append("\t The server : " + content + "\n");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
// Thread- Threads
this.setVisible(true);
}
public static void main(String[] args) {
new QQClientChat();
}
}
边栏推荐
- Panel for measuring innovation efficiency of 31 provinces in China (using Malmquist method)
- Onnx+tensorrt+yolov5: yolov5 deployment based on trt+onnx 1
- Explode and implode in PHP
- English grammar_ Frequency adverb
- The most complete ongdb and neo4j resource portal in history
- Spark Foundation
- MySQL learning summary 8: addition, deletion and modification of data processing
- Scala sets (array, list, set, map, tuple, option)
- PHP import classes in namespace
- Spark Optimization -- differences and policy selection of RDD cache (cache, persist, checkpoint)
猜你喜欢
[azure data platform] ETL tool (8) - ADF dataset and link service
Understand the difference between reducebykey and groupbykey in spark
[azure data platform] ETL tool (9) -- ADF performance optimization case sharing (1)
Simulink code generation: table lookup module and its code
Figure data * reconstruction subgraph
Nuggets new oil: financial knowledge map data modeling and actual sharing
Onnx+tensorrt+yolov5: yolov5 deployment based on trt+onnx 1
Two Chinese vector map data with map review number
Summary of the latest rail transit (Subway + bus) stops and routes in key cities in China (II)
LVS四层负载均衡集群(5)LVS概述
随机推荐
[azure data platform] ETL tool (7) - detailed explanation of ADF copy data
CXGRID keeps the original display position after refreshing the data
视频播放屡破1000W+,在快手如何利用二次元打造爆款
Cross border M & a database: SDC cross border database, Thomson database, A-share listed company M & a database and other multi index data (4w+)
Use cypher to get the tree of the specified structure
[azure data platform] ETL tool (9) -- ADF performance optimization case sharing (1)
Use PHP to count command line calls on your computer
2016. maximum difference between incremental elements
Spark optimization - Performance (general performance, operator, shuffle, JVM) tuning
Filters in PHP
Masa Auth - SSO and Identity Design
The latest collation of the number of years of education per capita in the country and provinces -1989-2020- includes the annual original data, calculation process and result summary
MySQL imports and exports multiple libraries at one time
(9) Explain broadcasting mechanism in detail
Sparksql of spark
Druid query
Azure SQL db/dw series (11) -- re understanding the query store (4) -- Query store maintenance
Union, intersection and difference sets of different MySQL databases
Doris data aggregation
Alibaba cloud OSS access notes