分享

Java实现FTP点对点传输文件

 yespon 2017-04-04

Java实现C/S结构的FTP点对点传输文件,写成了服务端和客户端的形式:先来看Server端的代码是如何写的:

01package ftp2;
02import java.io.*;
03import java.net.*;
04public class ServerFTP {
05    int port=8083;
06    String filepath="D:\\123.txt";
07    void start(){
08        Socket s=null;
09        try {
10            ServerSocket ss=new ServerSocket(port);
11            while(true){
12                File file=new File(filepath);
13                if(!file.exists()){
14                    file.createNewFile();
15                }
16                System.out.println("文件长度:"+(int)file.length());
17                s=ss.accept();
18                System.out.println("建立socket连接");
19                DataInputStream dis=new DataInputStream(new BufferedInputStream(newFileInputStream(filepath)));
20                DataOutputStream dos=new DataOutputStream(s.getOutputStream());
21                dos.writeUTF(file.getName());
22                dos.writeLong((long)file.length());
23                int bufferSize=8192;
24                byte[] buf=new byte[bufferSize];
25                while(true){
26                    int read=0;
27                    if(dis!=null){
28                        read=dis.read(buf);
29                    }
30                    if(read==-1){
31                        break;
32                    }
33                    dos.write(buf,0,read);
34                }
35                dis.close();
36                s.close();
37                System.out.println("文件传输完毕!!");
38            }
39        catch (Exception e) {
40            e.printStackTrace();
41        }
42    }
43    public static void main(String[] args) {
44        new ServerFTP().start();
45    }
46}

再来看下Client客户端文件的代码:

01package ftp2;
02import java.io.*;
03import java.net.*;
04public class ClientFTP {
05    private Socket s=null;
06    private String ip="localhost";
07    private int port=8082;
08    public ClientFTP() {
09        creationConnection();
10    }
11    private void creationConnection() {
12        try {
13            s=new Socket(ip,port);
14        catch (Exception e) {
15            // TODO: handle exception
16        }
17    }
18    private void getFile() {
19        String savePath="I:\\";
20        int bufferSize=8193;
21        byte[] buf=new byte[bufferSize];
22        int passedlen=0;
23        long length=0;
24        try {
25            DataInputStream dis=new DataInputStream(new BufferedInputStream(s.getInputStream()));
26            savePath+=dis.readUTF();
27            DataOutputStream dos=new DataOutputStream(new BufferedOutputStream(newFileOutputStream(savePath)));
28            length=dis.readLong();
29            System.out.println("文件长度:"+length+"\n");
30            while (true) {
31                int read=0;
32                if(dis!=null){
33                    read=dis.read(buf);
34                }
35                passedlen+=read;
36                if (read==-1) {
37                    break;                 
38                }
39                dos.write(buf,0,read);
40            }
41            System.out.println("接收文件另存为:"+savePath+"\n");
42            dos.close();
43        catch (Exception e) {
44            // TODO: handle exception
45        }
46    }
47    public static void main(String[] args) {
48        new ClientFTP().getFile();
49    }
50}

分别将上述两段代码保存为ServerFTP.java、ClientFTP.java,可在编译器中运行生成文件。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多