服务器端:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @program: ImportantNotDelete
* @Date: 2018-09-02 20:47
* @Author: 夜~星空
* @Description:服务器端:使用套接字实现文件下载功能
*/
public class Server {
public static void main(String[] args) {
try {
int i=0;
ServerSocket serverSocket=new ServerSocket(9992);
while(true){
System.out.println("服务器已启动!");
Socket socket =serverSocket.accept();
Thread thread=new Thread(new ThreadHandler(socket),"Thread-"+i++);
thread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ThreadHandler implements Runnable {
private Socket socket;
public ThreadHandler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
DataOutputStream dataOutputStream=null;
DataInputStream dataInputStream=null;
DataInputStream localRead =null;
try {
dataOutputStream=new DataOutputStream(socket.getOutputStream());
dataInputStream=new DataInputStream(new BufferedInputStream(socket.getInputStream()));
String filePath = dataInputStream.readUTF();
File file=new File(filePath);
//判断文件是否存在
if(!file.exists()){
return;
}else{
//文件名
String fileName = file.getName();
dataOutputStream.writeUTF(fileName);
dataOutputStream.flush();
//文件大小
long length = file.length();
dataOutputStream.writeUTF(String.valueOf(length));
dataOutputStream.flush();
System.out.println("开始向 "+Thread.currentThread().getName()+
" 发送文件,文件名:"+fileName+" 文件大小"+length);
localRead =new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
byte[] bytes=new byte[4096];
while (true){
int read=0;
if(localRead!=null){
read = localRead.read(bytes);
}
if(read==-1){
break;
}
dataOutputStream.write(bytes,0,read);
dataOutputStream.flush();
}
System.out.println("向 "+Thread.currentThread().getName()+" 发送文件完毕!");
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
localRead.close();
dataOutputStream.close();
dataInputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端:
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
/**
* @program: ImportantNotDelete
* @Date: 2018-09-02 20:28
* @Author: 夜~星空
* @Description:客户端:使用套接字进行文件下载
*/
public class Client {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
DataInputStream dateInputStream=null;
DataOutputStream dataOutputStream=null;
DataOutputStream localWrite=null;
Socket socket=null;
try {
socket=new Socket("127.0.0.1",9992);
//输入流
dateInputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
//输出流
dataOutputStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
System.out.println("请输入文件路径:");
String filePath = in.next();
dataOutputStream.writeUTF(filePath);
dataOutputStream.flush();
String fileName = dateInputStream.readUTF();
String length=dateInputStream.readUTF();
System.out.println("文件名:"+fileName+" 文件大小:"+length);
localWrite=new DataOutputStream(new BufferedOutputStream(new FileOutputStream("F:/"+fileName)));
byte[] bytes=new byte[4096];
while (true){
int read=0;
if(dataOutputStream!=null){
read = dateInputStream.read(bytes);
}
if(read==-1){
break;
}
localWrite.write(bytes,0,read);
localWrite.flush();
}
System.out.println("文件传输完毕!");
} catch (IOException e) {
e.printStackTrace();
}finally {
in.close();
try {
localWrite.close();
dataOutputStream.close();
dateInputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}