服务器端
package com.cs;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class FileServer {
public static void main(String[] args) throws IOException {
ServerSocket ser = new ServerSocket(9999);
Socket sc = ser.accept();
System.out.println("发现有客户端接入:"+sc.getInetAddress());
//从sc中获取流,网络io流
InputStream in = sc.getInputStream();
OutputStream out = sc.getOutputStream();
//为了简化操作,创建过滤流
ObjectOutputStream oos = new ObjectOutputStream(out);
ObjectInputStream ois = new ObjectInputStream(in);
//源文件
File src = new File("D:/images/20160715173100.png");
oos.writeObject(src.getName());
System.out.println("向客户端发送文件名***");
//接收客户端已有文件大小
long overSize = ois.readLong();
System.out.println("收到客户端文件大小:"+overSize);
if(overSize==src.length()){//无需发送文件
oos.writeObject("bu");//给客户一个order,不需下载
oos.flush();
System.out.println("客户端文件完好,通知客户端,无需下载...");
}else{
oos.writeObject("xc");//给客户一个order,准备传输文件
System.out.println("告诉客户端,准备传送文件....");
//文件输入流,把文件读取到java中
InputStream fileStream = new FileInputStream(src);
//缓冲区大小
byte[] bs = new byte[1024*128];
//跳过客户端文件的长度,续传
fileStream.skip(overSize);
System.out.println("设置文件起始位置:"+overSize+",开始传送...");
//发送文件
for(int len= 0;(len=fileStream.read(bs))!=- 1;){
out.write(bs, 0, len);//写入网络
}
System.out.println("文件发送完成!!!");
fileStream.close();
}
sc.close();
ser.close();
System.out.println("服务器关闭");
}
}
客户端
package com.cs;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class FileClient {
public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException {
Socket socket = new Socket("192.168.1.45",9999);
System.out.println("成功连接到服务器");
//从sc中获取流,网络io流
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
//为了简化操作 ,创建过滤流
ObjectInputStream ois = new ObjectInputStream(in);
ObjectOutputStream oos = new ObjectOutputStream(out);
//接收文件名
String fileName = (String) ois.readObject();
System.out.println("接收文件名file:"+fileName);
File xin = new File("d:/cert",fileName);
//告诉服务器文件大小,服务器进行比较
oos.writeLong(xin.length());
oos.flush();//刷新缓冲区,强制缓冲区数据流出
System.out.println("向服务器发送文件大小...");
//接收服务器响应:xc代表续传,bu不传
String order = (String) ois.readObject();
System.out.println("接到服务器指令:"+order);
if("xc".equals(order)){
OutputStream fileOut = new FileOutputStream(xin,true);
System.out.println("开始接收文件"+xin.getAbsolutePath());//获取文件的路径
byte[] bs= new byte[1024*128];
for(int len= 0;(len=in.read())!=- 1;){
System.out.println(len);
fileOut.write(bs, 0, len);//从网络读取,写入到文件
}System.out.println("文件接收完成!!!");
fileOut.close();
}else if("bu".equals(order)){
System.out.println("文件妥妥的,无需下载!!");
}
socket.close();
}
}