⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtfsend.java

📁 ftp的传输 比较简单的设计 适合初学者 自己看啊 tmt
💻 JAVA
字号:

import java.io.*;
import java.net.Socket;

/**
 * Created by IntelliJ IDEA.
 * User: cxwu
 * Date: 2007-9-30
 * Time: 9:36:39
 * To change this templRTFSendate use File | Settings | File Templates.
 */
public class RTFSend extends Thread{
    private File sendFile;//用户选择的文件
    private Socket socket;
    private DataInputStream bin;
    private DataOutputStream bout;

    public RTFSend(File sendFile) {
        this.sendFile = sendFile;
        //初始化socket及其相关的输入输出流
        try {
            socket = new Socket("localhost",5800);
            bin = new DataInputStream(
                      // new BufferedInputStream(
                               socket.getInputStream());
            bout = new DataOutputStream(
                               socket.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void run() {
        //send fileName
         System.out.println(socket);
        try {
            //把文件名发送到接收方
            bout.writeUTF(sendFile.getName());
            System.out.println(socket);
            System.out.println("send name" + sendFile.getName());
            //判断接收方是否同意接收
            boolean isAccepted = bin.readBoolean();
            //如果同意接收则开始发送文件
            if(isAccepted){
                System.out.println("begin send file");
                BufferedInputStream fileIn = new BufferedInputStream(new FileInputStream(sendFile));
                byte[] buf = new byte[2048];
                int num = fileIn.read(buf);
                while(num != -1){
                    bout.write(buf,0,num);
                    bout.flush();
                    num = fileIn.read(buf);
                }
                fileIn.close();
                System.out.println("Send file finished:" + sendFile.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                bin.close();
                bout.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -