Java使用RandomAccessFile实现断点续传

发送端


import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.Socket;


public class SendFile extends Thread {

    private Socket socket = null;
    private DataOutputStream dos;
    private DataInputStream dis;
    private RandomAccessFile rad;

    public SendFile() {

        try {
            socket = new Socket("localhost", 8080);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void run() {

        try {
            String path = "E:\\迅雷下载\\LaoMaoTao_v9.5_1812.zip";
            dos = new DataOutputStream(socket.getOutputStream());
            dis = new DataInputStream(socket.getInputStream());
            rad = new RandomAccessFile(path, "r");
            File file = new File(path);
            String filename = file.getName();
            dos.writeUTF(filename);//传输文件名
            dos.flush();
            String rsp = dis.readUTF();//获取响应状态
            if (rsp!=null&&rsp.equals("ok")) {
                long receiveFileSize = dis.readLong();//读取文件已发送的大小
                dos.writeLong(rad.length());
                dos.flush();
                int length;
                if (receiveFileSize < rad.length()) {
                    byte[] buf = new byte[1024];
                    System.err.println("发送文件传输");
                    rad.seek(receiveFileSize);
                    while ((length = rad.read(buf)) > 0) {
                        dos.write(buf, 0, length);
                        dos.flush();
                    }
                }
                else {
                    System.err.println("文件已存在");
                }
            } else {
                System.err.println("文件传输失败");
            }
        } catch (IOException e) {

        } finally {
            try {
                dis.close();
                dos.close();
                rad.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    }


}

接收端


import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.ServerSocket;
import java.net.Socket;


public class ReceiveFile extends Thread {

    private ServerSocket connectSocket = null;
    private Socket socket = null;
    private DataInputStream dis;
    private DataOutputStream dos;
    private RandomAccessFile rad;


    public void run() {
        try {
            connectSocket = new ServerSocket(8080);
            socket = connectSocket.accept();
            dis = new DataInputStream(socket.getInputStream());
            dos = new DataOutputStream(socket.getOutputStream());
            if (true) {
                String filename = dis.readUTF();//第一次交互查看文件名
                System.err.println(filename);
                dos.writeUTF("ok");//交互确认信息
                dos.flush();
                rad = new RandomAccessFile(filename, "rw");
                ReceiveFile receiveFile = new ReceiveFile();
                Long receiveFileSize = receiveFile.selectFile(filename);//查看已下载的文件大小
                dos.writeLong(receiveFileSize);//发送已接收的大小
                dos.flush();
                long sendFileSize = dis.readLong();//已发送出来的文件大小
                //如果文件已存在则重新下载
                if (receiveFileSize >= sendFileSize) {
                    System.err.println("文件已存在");
                }
                else {
                    rad.seek(receiveFileSize);
                    int length;
                    byte[] buf = new byte[1024];
                    while ((length = dis.read(buf)) != -1) {
                        rad.write(buf, 0, length);
                    }//接收文件
                    System.out.println("end");
                }

            }

        } catch (IOException e) {

        } finally {
            try {
                dis.close();
                dos.close();
                rad.close();
                socket.close();
                connectSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public  Long selectFile(String filename) {
        Long fileLength = 0L;
        File file = new File(filename);
        if (file.exists() && file.isFile()) {
            fileLength = file.length();
        }

        return fileLength;
    }



}

接收测试类


public class FileReceiveTest{//接收方

    public static void main(String[] args) {
      
        ReceiveFile rf=new ReceiveFile();
        rf.start();
    }

}

发送测试类


public class FileSendTest{//发送方

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SendFile sf=new SendFile();
        sf.start();
    }

}

在文件传输的过程中中断程序,再次启动的时候可以继续下载
读者在使用的时候注意修改文件路径

你可能感兴趣的:(Java,IO,Java后台)