java Socket 文件传输

客户端:

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * 文件发送客户端主程序
 * @author admin_Hzw
 *
 */
public class BxClient {
	
	/**
	 * 程序main方法
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		int length = 0;
		double sumL = 0 ;
		byte[] sendBytes = null;
		Socket socket = null;
		DataOutputStream dos = null;
		FileInputStream fis = null;
		boolean bool = false;
		try {
			File file = new File("D:/天啊.zip"); //要传输的文件路径
			long l = file.length(); 
			socket = new Socket();  
			socket.connect(new InetSocketAddress("127.0.0.1", 48123));
			dos = new DataOutputStream(socket.getOutputStream());
			fis = new FileInputStream(file);      
			sendBytes = new byte[1024];  
			while ((length = fis.read(sendBytes, 0, sendBytes.length)) > 0) {
				sumL += length;  
				System.out.println("已传输:"+((sumL/l)*100)+"%");
				dos.write(sendBytes, 0, length);
				dos.flush();
			} 
			//虽然数据类型不同,但JAVA会自动转换成相同数据类型后在做比较
			if(sumL==l){
				bool = true;
			}
		}catch (Exception e) {
			System.out.println("客户端文件传输异常");
			bool = false;
			e.printStackTrace();  
		} finally{  
			if (dos != null)
				dos.close();
			if (fis != null)
				fis.close();   
			if (socket != null)
				socket.close();    
		}
		System.out.println(bool?"成功":"失败");
	}
}

服务端:

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
import com.boxun.util.GetDate;


/**
 * 接收文件服务
 * @author admin_Hzw
 *
 */
public class BxServerSocket {
	
	/**
	 * 工程main方法
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			final ServerSocket server = new ServerSocket(48123);
			Thread th = new Thread(new Runnable() {
				public void run() {
					while (true) {
						try {  
							System.out.println("开始监听...");
							/*
							 * 如果没有访问它会自动等待
							 */
							Socket socket = server.accept();
							System.out.println("有链接");
							receiveFile(socket);
						} catch (Exception e) {
							System.out.println("服务器异常");
							e.printStackTrace();
						}
					}
				}
			});
			th.run(); //启动线程运行
		} catch (Exception e) {
			e.printStackTrace();
		}     
	}

	public void run() {
	}

	/**
	 * 接收文件方法
	 * @param socket
	 * @throws IOException
	 */
	public static void receiveFile(Socket socket) throws IOException {
		byte[] inputByte = null;
		int length = 0;
		DataInputStream dis = null;
		FileOutputStream fos = null;
		String filePath = "D:/temp/"+GetDate.getDate()+"SJ"+new Random().nextInt(10000)+".zip";
		try {
			try {
				dis = new DataInputStream(socket.getInputStream());
				File f = new File("D:/temp");
				if(!f.exists()){
					f.mkdir();  
				}
				/*  
				 * 文件存储位置  
				 */
				fos = new FileOutputStream(new File(filePath));    
				inputByte = new byte[1024];   
				System.out.println("开始接收数据...");  
				while ((length = dis.read(inputByte, 0, inputByte.length)) > 0) {
					fos.write(inputByte, 0, length);
					fos.flush();    
				}
				System.out.println("完成接收:"+filePath);
			} finally {
				if (fos != null)
					fos.close();
				if (dis != null)
					dis.close();
				if (socket != null)
					socket.close(); 
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

时间工具类:

import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * 时间工具类
 * @author admin_Hzw
 *
 */
public class GetDate {
	/**
	 * 时间格式到毫秒
	 */
	private static SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
	public static String getDate(){
		return df.format(new Date());
	}  
}



  • 14
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

BUG胡汉三

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

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

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

打赏作者

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

抵扣说明:

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

余额充值