只发代码,有注释的了,应该能看懂的。(JAVA)
服务器端代码
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private int port = 8821;
private Socket socket = null;
public void start(){
Thread thread = null;
try{
ServerSocket server = new ServerSocket(port);
while(true){
System.out.println("Server类 启动服务器");
// public Socket accept() throws
// IOException侦听并接受到此套接字的连接。此方法在进行连接之前一直阻塞。
socket = server.accept();
System.out.println("建立socket连接");
thread = new Thread(new SocketRun(socket));
thread.start();
}
} catch (Exception e){
e.printStackTrace();
} finally {
if(socket != null){
try{
socket.close();
}catch(Exception e){
socket = null;
System.out.println("服务端 finally 异常:" + e.getMessage());
}
}
}
}
public static void main(String[] args) {
Server server = new Server();
server.start();
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class SocketRun implements Runnable{
private Socket socket = null;
public SocketRun(Socket socket){
this.socket = socket;
}
@Override
public void run() {
System.out.println("----------------------------");
DataInputStream input = null;
DataOutputStream out = null;
DataOutputStream fileOut = null;
try{
//接受客户端发送过来的消息
input = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
//接收文件名
String fileName = input.readUTF();
System.out.println("客户端发送过来的内容 文件名为:" + fileName);
//接收文件大小
Long fileSize = input.readLong();
System.out.println("文件大小为:" + fileSize);
//接收文件数据
String savePath = "C:\\Users\\Administrator\\Desktop\\新建文件夹\\1111\\" + fileName;
fileOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(savePath)));
byte[] buf = new byte[1024];
while(true){
int read = 0;
if(input != null){
read = input.read(buf,0,buf.length);//为了保证文件能成功复制,输入的时候是什么格式,输出的时候也用什么格式
}
if(read == -1){
break;
}
fileOut.write(buf, 0, read);
}
fileOut.close();
//向客户端回复消息
out = new DataOutputStream(socket.getOutputStream());
String s = "accept end";
out.writeUTF(s);
}catch(Exception e){
e.printStackTrace();
System.out.println(Thread.currentThread() + "出错:" + e.getMessage());
}finally{
try {
if(out != null)out.close();
if(input != null)input.close();
if(socket != null){
socket.close();
socket = null;
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("服务端 finally 异常:" + e.getMessage());
}
}
}
public void setSocket(Socket socket) {
this.socket = socket;
}
}
客户端
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
public class Client {
public final String IP_ADDR = "localhost";//服务器地址
public final int PORT = 8821;//服务器端口号
public static void main(String[] args) {
new Client().uploadFile("111.png");
new Client().uploadFile("222.png");
new Client().uploadFile("333.png");
}
/**
* 从服务器读取文件
*/
public void downloadFile(){
}
/**
* 上传文件到服务器
* @param name
*/
public void uploadFile(String name) {
DataInputStream input = null;
DataOutputStream out = null;
DataInputStream fileInput = null;
Socket socket = null;
try {
File file = new File("C:\\Users\\Administrator\\Desktop\\新建文件夹\\" + name);
long fileSize = file.length();
String fileName = file.getName();
//创建一个流套接字并将其连接到指定主机上的指定端口号
socket = new Socket(IP_ADDR, PORT);
//向服务器端发送数据
out = new DataOutputStream(socket.getOutputStream());
//向服务器端发送文件名
out.writeUTF(fileName);
out.flush();
//向服务器端发送文件大小
out.writeLong(fileSize);
out.flush();
//向服务器端发送 文件数据
//读取文件内容
fileInput = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
byte[] bup = new byte[1024];
while(true){
int read = 0;
if(fileInput != null){
read = fileInput.read(bup,0,bup.length);
}
if(read == -1){
break;
}
out.write(bup, 0, read);
}
out.flush();
out.close();
//读取服务器端数据
input = new DataInputStream(socket.getInputStream());
String ret = input.readUTF();
// 如接收到 "accept end" 则断开连接
if ("accept end".equals(ret)) {
System.out.println("客户端将关闭连接");
}
} catch (Exception e) {
System.out.println("客户端异常:" + e.getMessage());
} finally {
try {
if(input != null) input.close();
if(fileInput != null) fileInput.close();
if (socket != null) {
socket.close();
System.out.println("socket is closed");
socket = null;
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("客户端 finally 异常:" + e.getMessage());
}
}
}
}