java 文件传输 内存缓存 100M



import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Date;


public class client {


        private static final int Server_PORT = 6000;
        private static final int Client_PORT = 6001;


        /**
         * 使用方法:运行这个程序需要带上参数,参数类型为点分十进制的ip地址,例如:192.168.0.153
         *
         * @param args
         * @throws IOException
         */
        public static void main(String[] args) throws IOException {


                System.out.println("This is client");


                String ipStr = "192.168.2.41";
                try {
                        // 创建一个Socket
                        File file = new File("hadoop-2.0.0-cdh4.1.2.tar.gz");
                        Long filelength = file.length();
                        Socket s = new Socket();
                        s.setSendBufferSize(filelength.intValue());




                        s.connect(new InetSocketAddress(ipStr, Server_PORT), Client_PORT);
                        OutputStream os = s.getOutputStream();// 输出流


                        InputStream is = s.getInputStream();// 输入流




                        byte[] filecontent = new byte[filelength.intValue()];
                        try {
                                FileInputStream in = new FileInputStream(file);
                                in.read(filecontent);
                                in.close();
                        } catch (FileNotFoundException e) {
                                e.printStackTrace();
                        } catch (IOException e) {
                                e.printStackTrace();
                        }


                        Date date = new Date();
                        System.out.println("time:"+date.getTime());


            os.write(filecontent);
            Date date2=new Date();
            System.out.println("time:"+date2.getTime());
            System.out.println(date2.getTime()-date.getTime());





                        System.out.println("\nFile has been sent successfully.");
                        os.close();
                        is.close();
                        s.close();
                } catch (Exception ex) {
                        ex.printStackTrace();
                }
        }




}


























import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Date;


public class client2 extends Thread {


        private static final int Server_PORT = 6000;
        private static final int Client_PORT1 = 6001;


        String ip;
        private static final int Client_PORT2 = 6002;
        int clientPort;


        public client2(String ip,int clientPort){
                this.ip=ip;
                this.clientPort=clientPort;
        }
        public void run() {


                System.out.println("This is client");


                try {
                        // 创建一个Socket
                        File file = new File("hadoop-2.0.0-cdh4.1.2.tar.gz");
                        Long filelength = file.length();
                        Socket s = new Socket();
                        s.setSendBufferSize(filelength.intValue());




                        s.connect(new InetSocketAddress(ip, Server_PORT), clientPort);
                        OutputStream os = s.getOutputStream();// 输出流


                        InputStream is = s.getInputStream();// 输入流




                        byte[] filecontent = new byte[filelength.intValue()];
                        try {
                                FileInputStream in = new FileInputStream(file);
                                in.read(filecontent);
                                in.close();
                        } catch (FileNotFoundException e) {
                                e.printStackTrace();
                        } catch (IOException e) {
                                e.printStackTrace();
                        }


                        Date date = new Date();
                        System.out.println("time:"+date.getTime());


            os.write(filecontent);


            Date date2 = new Date();
                        System.out.println("time:"+date2.getTime());
                        System.out.println(date2.getTime()-date.getTime());




                        System.out.println("\nFile has been sent successfully.");
                        os.close();
                        is.close();
                        s.close();
                } catch (Exception ex) {
                        ex.printStackTrace();
                }


        }
        public static void main(String[] args) throws IOException {
                new client2("192.168.2.41",Client_PORT1).start();
                new client2("192.168.2.42",Client_PORT2).start();


        }




}






























import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;


/**
 * 一个简单的多线程服务器程序,用于传输文件
 * 
 */
public class server extends Thread {
public static void main(String[] args) {
serverStart();// 启动服务器程序


}


private static final int PORT = 6000;
private Socket s;


public server(Socket s) {
this.s = s;
}


public void run() {
try {
File file = new File("hadoop-2.0.0-cdh4.1.2.tar.gz");
Long filelength = file.length();
s.setReceiveBufferSize(filelength.intValue());
OutputStream os = s.getOutputStream();
InputStream is = s.getInputStream();


byte[] filecontent = new byte[filelength.intValue()];


Date date = new Date();
System.out.println("time:" + date.getTime());


int temp=0;
while(temp<filelength)
{
temp+=is.read(filecontent);
System.out.println(temp);
}
Date date2 = new Date();
System.out.println("time:" + date2.getTime());


                        System.out.println(date2.getTime()-date.getTime());


// FileOutputStream fins = new FileOutputStream("saveFile");
// fins.write(filecontent);


os.close();
is.close();
s.close();
} catch (Exception e) {
e.printStackTrace();
}


}




public static void serverStart() {
System.out.println("This is server");
try {
ServerSocket ss = new ServerSocket(PORT);
int count = 0;
while (true) {
// 创建一个Socket等待客户端连接


Socket s = ss.accept();
count++;
System.out.println("This is the " + count
+ "'st client connetion!");
new server(s).start();// 启动一个线程为这个客户端服务


}
} catch (Exception ex) {
ex.printStackTrace();
}
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FocusOneThread

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值