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();}}}

旅行,不要害怕错过什么,

java 文件传输 内存缓存 100M

相关文章:

你感兴趣的文章:

标签云: