文件的上传和下载
程序员文章站
2022-04-30 21:10:25
...
01、黑马词霸
客户端
import java.io.*;
import java.net.Socket;
//《黑马词霸》 客户端
//"hello" -> 客户端 -发送->服务端 ->查阅词典->返回释义->回执给客户端
public class ClientDemo {
public static void main(String[] args) throws IOException {
System.out.println("客户端");
//单词
String word = "java";
//[1]准备发送数据的操作
Socket clinetSocket = new Socket("192.168.25.75", 8888);
//获取IO流的对象
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(clinetSocket.getOutputStream()));
//写数据
bw.write(word);
bw.newLine();
bw.flush();
//----------------------------------
//接收数据.从客户端的clientSocket当中获取流对象(输入流)
BufferedReader br = new BufferedReader(new InputStreamReader(clinetSocket.getInputStream()));
String value = br.readLine();
//打印结果
System.out.println("【服务器说】" + value);
//释放资源
br.close();
bw.close();
clinetSocket.close();
}
}
服务端
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
//《黑马词霸》 服务端
//"hello" -> 客户端 -发送->服务端 ->查阅词典->返回释义->回执给客户端
public class ServerDemo {
public static void main(String[] args) throws IOException{
//创建生词本
Map<String,String> map = new HashMap<>();
map.put("hello","雷浩啊");
map.put("world","世界");
map.put("niubi","牛掰");
//===========================================
System.out.println("服务端开启鸟!~~");
//创建服务端的ServerSocket的对象
ServerSocket serverSocket = new ServerSocket(8888);
//获取客户端的Socket
Socket clientSocket = serverSocket.accept();
//获取IO流
BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//获取数据
String word = br.readLine();
//查找数据
String value = map.get(word);
//判断
if(value == null){
value = "服务器正在升级维护当中...无法匹配";
}
//-----------------
//回执数据.从客户端的clientSocket当中获取流对象(输出流)
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
//写数据
bw.write(value);
bw.newLine();
bw.flush();
//释放资源
bw.close();
br.close();
clientSocket.close();
}
}
02、图片上传(多线程版本)
客户端
import java.io.*;
import java.net.Socket;
//上传图片到服务器【客户端】
public class UploadClinetDemo {
public static void main(String[] args) throws IOException{
System.out.println("客户端");
//准备输入流.读取硬盘的数据 图片
FileInputStream fis = new FileInputStream("day11-classCode\\客户端硬盘\\真帅.PNG");
//创建Socket的对象
Socket clientSocket = new Socket("192.168.25.75",10086);
//获取流对象
OutputStream os = clientSocket.getOutputStream();
//--------------------------
//读写数据
int len;
byte[] buff = new byte[1024];
while((len = fis.read(buff))!=-1){
os.write(buff,0,len);
}
//*******************************
//通知服务端.流写完啦.!!!
clientSocket.shutdownOutput();
//*******************************
//---------------------------
//接收数据(来自于服务器回复的数据)
BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String ss = br.readLine();
System.out.println(ss);
//释放资源
br.close();
os.close();
fis.close();
clientSocket.close();
}
}
服务端
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
//上传图片到服务器【服务端】
public class UploadServerDemo {
public static void main(String[] args) throws IOException {
System.out.println("服务器开启鸟!~~");
//创建服务端的ServerSocket的对象
ServerSocket serverSocket = new ServerSocket(10086);
while (true) {
//获取客户端的Socket
Socket clientSocket = serverSocket.accept();
new Thread(()->{
try {
method(clientSocket);
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
}
public static void method(Socket clientSocket) throws IOException {
//获取IO流
InputStream is = clientSocket.getInputStream();
//--------------------
//查阅IP地址的操作
String address = clientSocket.getInetAddress().getHostAddress();
System.out.println(address);
//获取当前的系统时间
long time = System.currentTimeMillis();
String name = "day11-classCode\\服务端硬盘\\" + time + ".PNG";
//创建输出流对象
FileOutputStream fos = new FileOutputStream(name);
//读写数据
int len;
byte[] buff = new byte[1024];
while ((len = is.read(buff)) != -1) {
fos.write(buff, 0, len);
}
System.out.println("上传图片成功");
//------------------------------------
//输出流(服务端->客户端)
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
bw.write(name + "文件上传成功!");
bw.newLine();
bw.flush();
//释放资源
bw.close();
is.close();
fos.close();
clientSocket.close();
}
}