Client-Server发送文件

客户端向服务器端发送文件,客户端发送按钮,服务器端处于开启状态。

客户端:send函数+发送文件按钮事件处理;服务器端:recieve函数+开启的一条线程

客户端

//传输文件函数
public void send() {
byte[] sendBytes = null;
Socket socket = null;
DataOutputStream dos = null;
DataInputStream dis = null;
FileInputStream fis = null;
String s = filePathFiled.getText();//要传输的文件路径
boolean bool = false;
try {
File file = new File(s); //根据路径创建File对象
fixedL = file.length();//以字节为单位的文件长度
socket = new Socket();
socket.connect(new InetSocketAddress(this.IP, 8888));//8888端口,this.IP在构造函数中已被初始化为服务器IP
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
fis = new FileInputStream(file);
sendBytes = new byte[1024 * 10];
String type = s.substring(s.lastIndexOf("."));
dos.writeUTF(this.stuNo + "_" + this.stuName + type);// 发送部分1:为了创建一个文件对象,并命名完善
progressBar.setMaximum(100);
while ((length = fis.read(sendBytes, 0, sendBytes.length)) > 0) {
sumL += length;
progressBar.setValue((int) ((sumL / fixedL) * 100));//进度条设置进度、步步更新
dos.write(sendBytes, 0, length);//发送部分2:文件内容
dos.flush();
}
if (sumL == fixedL) {// 虽然数据类型不同,但JAVA会自动转换成相同数据类型后在做比较
bool = true;
sumL = fixedL = 0;//若要限定只能交一次,就是加上语句sendButton.setEnabled(false);
}
} catch (Exception e1) {
JOptionPane.showMessageDialog(contentPane, "系统找不到指定的文件!");
bool = false;
} finally {
try {
if (dos != null)
dos.close();
if (dis != null)
dis.close();
if (fis != null)
fis.close();
if (socket != null)
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
JOptionPane.showMessageDialog(contentPane, bool ? "上传成功!" : "上传失败!");
}

//事件处理

//发送文件及其监听器处理
sendButton = new JButton("\u53D1\u9001");
sendButton.addActionListener(new ActionListener() {//匿名内部类
public void actionPerformed(ActionEvent e) {
new Thread(new Runnable() {
public void run() {
send();
}//线程运行的目标类
}).start();//点击按钮启动传输线程,线程处于就绪态;运行则取决于操作系统
}
});
sendButton.setBounds(389, 34, 93, 23);//(x,y,宽,高)
contentPane.add(sendButton);

服务器端

//接收客户端上传的文件
public void recieve(Socket socket) throws IOException {
byte[] inputByte = null;
int length = 0;
DataInputStream dis = new DataInputStream(socket.getInputStream());
FileOutputStream fos = null;
String info = dis.readUTF();// 读取文件名字符串
String filePath = savePath.getText() + "/" +info;//存储文件的路径及文件名称
try {
File f = new File(this.savePath.getText());
if (!f.exists()) {
f.mkdir();
}
fos = new FileOutputStream(new File(filePath));
inputByte = new byte[1024];
while ((length = dis.read(inputByte, 0, inputByte.length)) != -1) {
fos.write(inputByte, 0, length);
fos.flush();
}
items.addElement(info);//接收成功一次,列表就会添加一项
}catch (Exception e) {
e.printStackTrace();
}finally {
if (fos != null)
fos.close();
if (dis != null)
dis.close();
if (socket != null)
socket.close();
}
}

 /**
* 构造时,启动两条线程,8888端口接收文件、9527消息传递收录服务器端产生的Socket对象,不同端口不同服务
* while循环一定要有,实现服务器开启时任何时候客户端连接服务器均可
* 又因为while中的accept语句具有阻塞功能,所以放到线程中。
*/
new Thread(new Runnable() {//这条线程用来循环
public void run() {//在循环中,每连接一个客户端就创建一个线程进行文件接收,达到同时上传的目的
try {
@SuppressWarnings("resource")
ServerSocket serverFile = new ServerSocket(8888);
while (true) {
final Socket socket = serverFile.accept();
new Thread(new Runnable() {
public void run() {
try {
recieve(socket);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}).start();

以上。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值