java socket传文件_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"); //要传输的62616964757a686964616fe59b9ee7ad9431333337396235文件路径

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

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值