使用是可以自己自行建立testIn.txt文件,服务器端代码如下:
package io2;

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

public class server2 {
         public static void main(String[] args) throws IOException {
                ServerSocket s = null;
                Socket s1;
                String filePath = "c:\\testIn.txt";
                OutputStream s1out;
                FileOutputStream dos;
                 //    通过5432端口建立连接

                s = new ServerSocket(5432);

                 //    循环运行监听程序,以监视连接请求
                 while ( true) {
                         //    监听端口请求,等待连接
                        s1 = s.accept();
                         //    得到与socket相连接的数据流对象
                        s1out = s1.getOutputStream();
                         //todo
                        File fi = new File(filePath);
                        DataInputStream fis = new DataInputStream( new BufferedInputStream(
                                         new FileInputStream(filePath)));
                        DataOutputStream ps = new DataOutputStream(s1.getOutputStream());
//将文件名及长度传给客户端。
                        ps.writeUTF(fi.getName());
                        ps.flush();
                        ps.writeLong(( long) fi.length());
                        ps.flush();
                         int bufferSize = 8192;
                         byte[] buf = new byte[bufferSize];
                         while ( true) {
                                 int read = 0;
                                 if (fis != null) {
                                        read = fis.read(buf);
                                }
                                 if (read == -1) {
                                         break;
                                }
                                ps.write(buf, 0, read);
                        }
                        ps.flush();

                        s1out.close();
                        s1.close();
                }

        }
}
客户端代码:
package io2;

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


public class client2 {
         public static void main(String[] args) throws IOException {
                Socket s1;
                InputStream s1In = null;
                DataInputStream dis = null;
                 int passedlen = 0;
                 long len = 0;
                 //    在端口5432打开连接
                s1 = new Socket( "foreman ", 5432);
                 // 获得socket端口的输入句柄,并从中读取据
                String savePath = "e:\\";
                 int bufferSize = 8192;
                 byte[] buf = new byte[bufferSize];

                s1In = s1.getInputStream();
                dis = new DataInputStream(s1In);
                savePath += dis.readUTF();
                DataOutputStream fileOut = new DataOutputStream( new BufferedOutputStream(
                                 new BufferedOutputStream( new FileOutputStream(savePath))));
                len = dis.readLong();
                System.out.println( "文件的长度为:" + len + "");
                System.out.println( "开始接收文件!" + "");
                 while ( true) {
                         int read = 0;
                         if (s1In != null) {
                                read = s1In.read(buf);
                        }
                        passedlen += read;
                         if (read == -1) {
                                 break;
                        }
                         //下面进度条本为图形界面的prograssBar做的,这里如果是打文件,
                         //可能会重复打印出一些相同的百分比
                        System.out.println( "文件接收了" + (passedlen * 100 / len) + "%");
                        fileOut.write(buf, 0, read);
                }
                System.out.println( "接收完成,文件存为" + savePath + "");
                fileOut.close();
        }
}
 
 
 
 
吉林省三扬科技咨询有限公司- 长春JAVA培训| Java培训| PHP培训| 3G培训| 长春三扬java培训| 长春培训| C++培训| 嵌入式培训| ORACLE培训| 软件测试培训|. Net培训| 游戏培训|嵌入式LINUX培训| 数据库培训| 大学生IT培训|| 软件培训| IT培训| IT就业培训| 软件工程师培训| 程序员培训| 电脑培训| IT认证培训| 大学生就业培训| 职业技能培训| IT业余班| 周末班培训