socket 多线程实现文件上传下载


  
//用JAVA SOCKET发送和接收文件

//Java代码  
/*服务器端接收文件*/
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFileChooser;
/**
* 该类用到的绑定端口初始为10000,如果绑定不成功则试另外的端口;
* 绑定次数用tryBindTimes变量,表示如果绑定失败会对它加一;
* 当前绑定端口由DefaultBindPort+tryBindTimes决定;
* 外界系统(调用此程序的对象)可以获取当前的帮定端口;
* 并告诉客户端服务的端口号以使其能正确连接到该端口上;
* @author
*
*/
public class GetFile extends Thread {
// 服务套接字等待对方的连接和文件发送
ServerSocket serSocket;
// 由服务套接字产生的 套接字
Socket tempSocket;
// 用于读取
InputStream inSocket;
// 随机访问文件
RandomAccessFile inFile = null;
// 临时缓寸区
byte byteBuffer[] = new byte[1024];
// 默认用10000端口监听请求
int defaultBindPort = 10000;
// 初始的绑定端口次数为0
int tryBindTimes = 0;
// 当前绑定的端口号是10000默认端口
int currentBindPort = defaultBindPort + tryBindTimes;
/**
* @构造方法
* @抛出异常的原因是无法绑定服务的端口
* */
public GetFile(int port) throws Exception {
try {
// 绑定服务的端口
this.bindToServerPort();
} catch (Exception e) {
e.printStackTrace();
// 绑定不成功重试
System.out.println(e.toString());
throw new Exception("绑定端口不成功!");
}
// 文件选择器以当前的目录打开
JFileChooser jfc = new JFileChooser(".");
jfc.showSaveDialog(new javax.swing.JFrame());
// 获取当前的选择文件引用
File savedFile = jfc.getSelectedFile();
// 已经选择了文件
if (savedFile != null) {
// 读取文件的数据,可以每次以快的方式读取数据
inFile = new RandomAccessFile(savedFile, "rw");
}
}
/**
* @监控线程
*/
public void run() {
try {
if (this.inFile == null) {
System.out.println("没有选择文件");
// 关闭服务方套接字
this.serSocket.close();
// 没有选择文件
return;
}
System.out.println("wait for..." + '\n' + "等待对方接入");
// 等待对方的连接
tempSocket = serSocket.accept();
// 五秒钟连不上将抛出异常
this.serSocket.setSoTimeout(5000);
// 获取输入流
this.inSocket = tempSocket.getInputStream();
} catch (Exception ex) {
System.out.println(ex.toString());
ex.printStackTrace();
return;
}
// 以下为传送文件代码和 套接字清理工作
int amount;
try {
while ((amount = inSocket.read(byteBuffer)) != -1) {
inFile.write(byteBuffer, 0, amount);
}
// 关闭流
inSocket.close();
javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(),
"已接收成功", "提示!", javax.swing.JOptionPane.PLAIN_MESSAGE);
System.out.println("Get OK");
System.out.println("接收完毕!");
// 关闭文件
inFile.close();
// 关闭临时套接字
tempSocket.close();
// 关闭服务方套接字
this.serSocket.close();
} catch (IOException e) {
System.out.println(e.toString());
e.printStackTrace();
}
}
/**
* @绑定端口
* @throws Exception 抛出异常的原因是无法绑定服务的端口
*/
private void bindToServerPort() throws Exception {
try {
// 输出绑定的端口号到当前的控制台上
System.out.println("试绑定的端口号是:" + this.currentBindPort);
// 在自己的机器上开一个服务类套接字并等待发送者的连接
serSocket = new ServerSocket(this.currentBindPort);
} catch (Exception e) {
e.printStackTrace();
// 绑定不成功重试
System.out.println(e.toString());
// 试了不止一次了
this.tryBindTimes = this.tryBindTimes + 1;
// 可查看试的次数getTryBindedTimes
this.currentBindPort = this.defaultBindPort + this.tryBindTimes;
// 如果试的次数超过20次 退出
if (this.tryBindTimes >= 20) {
throw new Exception("无法绑定到指定端口" + '\n' + "试了太多次了!");
}
// 递归的绑定
this.bindToServerPort();
}
// 输出绑定的端口号到当前的控制台上
System.out.println("成功绑定的端口号是: " + this.currentBindPort);
}
// 获取试绑定的端口
public int getTryBindedTimes() {
return this.tryBindTimes;
}
// 获取已经绑定的端口
public int getCurrentBindingPort() {
return this.currentBindPort;
}
/**
* @测试方法
* @param args
*/
public static void main(String args[]) {
GetFile getFile = null;
try {
getFile = new GetFile(10000);
} catch (Exception e) {
e.printStackTrace();
System.out.println("无法传送文件!");
System.exit(1);
}
getFile.start();
}
}
Java代码  
/*文件发送端*/
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.net.Socket;
import javax.swing.JFileChooser;
/**
* 在服务器端开启的情况下 实例化套接字 并发送文件
*
* @author
*/
public class SendFile extends Thread {
// 远程的IP字符串
String remoteIPString = null;
// 远程的服务端口
int port;
// 临时套接字
Socket tempSocket;
// 发送文件用的输出流
OutputStream outSocket;
// 欲发送的文件
RandomAccessFile outFile;
// 发送文件用的临时缓存区
byte byteBuffer[] = new byte[1024];
/**
* 构造方法仅用于选择发送文件的位置 并从外部接收远程地址和端口
*
*/
public SendFile(String remoteIPString, int port) {
try {
this.remoteIPString = remoteIPString;
this.port = port;
// 选择发送的文件位置
JFileChooser jfc = new JFileChooser(".");
File file = null;
int returnVal = jfc.showOpenDialog(new javax.swing.JFrame());
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = jfc.getSelectedFile();
}
outFile = new RandomAccessFile(file, "r");
} catch (Exception e) {
}
}
/**
* 先决条件是服务器端先开启
*
*/
public void run() {
try {
this.tempSocket = new Socket(this.remoteIPString, this.port);
System.out.println("与服务器连接成功!");
outSocket = tempSocket.getOutputStream();
int amount;
System.out.println("开始发送文件...");
while ((amount = outFile.read(byteBuffer)) != -1) {
outSocket.write(byteBuffer, 0, amount);
System.out.println("文件发送中...");
}
System.out.println("Send File complete");
javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(),
"已发送完毕", "提示!", javax.swing.JOptionPane.PLAIN_MESSAGE);
outFile.close();
tempSocket.close();
} catch (IOException e) {
System.out.println(e.toString());
e.printStackTrace();
}
}
/**
* 测试方法
*
* @param args
*/
public static void main(String args[]) {
SendFile sf = new SendFile("127.0.0.1", 10000);
sf.start();
}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值