现在的位置: 首页 > 综合 > 正文

简单socket编程,传输文件,JAVA

2017年12月04日 ⁄ 综合 ⁄ 共 3784字 ⁄ 字号 评论关闭

package socket_file;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class server {
private static int port = 2100;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String filePath = "d:\\b.jar";
		File file = new File(filePath);
		try {
			start(file);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static void start(File file) throws IOException {
		// TODO Auto-generated method stub
		ServerSocket server = new ServerSocket(port);
		while(true){
			Socket socket = server.accept();//在这个地方阻塞
			new Thread(new Task1(file,socket)).start();
		}
	}
	static class Task1 implements Runnable{
		private File file;
		private Socket socket;
		Task1(File file,Socket socket){
			this.file = file;
			this.socket = socket;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			try {
				DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
				DataOutputStream out = new DataOutputStream(socket.getOutputStream());
				out.writeUTF(file.getName());
				out.flush();
				out.writeInt((int) file.length());
				out.flush();
				int bufferSize = 1024;
				byte[] size = new byte[bufferSize];
				while(true){
					int read = 0;
					if(in!=null){
						read = in.read(size);
						System.out.println("读取长度"+read);
					}
					if(read == -1){
						break;
					}
					out.write(size,0,read);
				}
				out.flush();
				socket.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

}


package socket_file;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class Util {
	private String ip;
	private int port;
	private Socket socket = null;
	private DataInputStream in;
	private DataOutputStream out;

	Util(String ip, int port) {
		this.ip = ip;
		this.port = port;
	}

	public boolean getConnection() {
		try {
			if (socket != null) {
				socket.close();
			}
			socket = new Socket(ip, port);
			return true;
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * @param args
	 * @throws IOException
	 */
	public DataInputStream getInformation() throws IOException {
		in = new DataInputStream(new BufferedInputStream(socket
				.getInputStream()));
		return in;

	}
}


package socket_file;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.FilePermission;
import java.io.IOException;
import java.net.Socket;

public class client {

	/**
	 * @param args
	 */
	 
	private Util socket = null;
	private String ip = "localhost";
	private int port = 2100;
	Util util;

	public client() throws IOException {
		if (isConnected()) {
			getMessage();
		}
	}

	private void getMessage() throws IOException {
		// TODO Auto-generated method stub
		if (socket == null) {
			return;
		}
		DataInputStream in = null;
		try {
			in = socket.getInformation();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("error");
			return;
		}
		String savePath = "e:\\";
		int bufferSize = 1024;
		byte[] size = new byte[bufferSize];

		long length;
		savePath += in.readUTF();
		DataOutputStream out = new DataOutputStream(new BufferedOutputStream(
				new FileOutputStream(savePath)));
		length = in.readLong();
		System.out.println("文件长度:" + length + "\n");
		System.out.println("开始接收文件" + "\n");
		while (true) {
			int temp = 0;
			if (in != null) {
				temp = in.read(size);
			}
			 
			if (temp == -1) {
				break;
			}
			 
			out.write(size, 0, (int) temp);
		}
		System.out.println("接收完成,存储在" + savePath + "中");
		out.close();
	}

	public boolean isConnected() {
		socket = new Util(ip, port);
		if (socket.getConnection())
			return true;
		else
			return false;
	}

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		new client();
	}

}

抱歉!评论已关闭.