发送端:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Inet4Address;
import java.net.SocketException;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class LoginJiemian {
public static void main(String[] args) throws FileNotFoundException {
new FileSend().FileSen();
}
}
class FileSend{
JFrame jFrame;
JPanel InputPanel;
JLabel PortNumberLabel;
JButton buttonChooseFile,buttonTransportFile;
JTextField textPortNumber;
File file;
byte[] buffer,bufferPacketNum,bufferPacketContent,receiveByte;
DatagramPacket packet,packet1;
DatagramSocket socket;
DataInputStream fis;
int read;
long a;
int Num;
String str;
FileSend() throws FileNotFoundException{
try {
socket = new DatagramSocket(9);
String filePath = "e:\\mysql.rar";
fis = new DataInputStream(new BufferedInputStream(
new FileInputStream(filePath)));
} catch (SocketException e) {
e.printStackTrace();
}
FileSen();
}
public void FileSen(){
jFrame = new JFrame("UDP文件传输");
jFrame.setBounds(400, 200,400, 400);
jFrame.setLayout(null);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
InputPanel=new JPanel();
InputPanel.setBounds(0,0,400, 400);
InputPanel.setLayout(null);
//两个功能按钮
buttonChooseFile=new JButton("选择文件");
buttonChooseFile.setBounds(20, 10, 100, 28);
buttonTransportFile=new JButton("发送");
buttonTransportFile.setBounds(20, 50, 100, 28);
//文本输入框 输入端口号
textPortNumber=new JTextField();
textPortNumber.setBounds(200, 10, 130, 20);
//标签 提示输入端口号
PortNumberLabel=new JLabel("端口号");
PortNumberLabel.setBounds(200, 50, 130, 20);
InputPanel.add(buttonChooseFile);
InputPanel.add(buttonTransportFile);
InputPanel.add(textPortNumber);
InputPanel.add(PortNumberLabel);
jFrame.add(InputPanel);
jFrame.setVisible(true);
buttonChooseFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
buttonTransportFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
receiveByte = new byte[20];
bufferPacketNum=new byte[20];
bufferPacketContent=new byte[1024];
buffer=new byte[1080];
Num=1;
a = new Date().getTime();
read = 0;
if (fis != null) {
try {
read = fis.read(bufferPacketContent);
buffer=byteMerger(intToByteArray(Num),bufferPacketContent);
packet = new DatagramPacket(buffer,0,buffer.length,
(Inet4Address) Inet4Address.getByName("localhost"), 8);
packet1=packet;//备份重传
socket.send(packet);
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (read == -1) {
System.out.print("用时:");
System.out.println(new Date().getTime() - a);
return;
}
while (true) {
packet = new DatagramPacket(receiveByte, receiveByte.length);
socket.receive(packet);
if (fis != null) {
str = new String(packet.getData(),0,packet.getLength());
System.out.println(str);
if(str.equals(new String().valueOf(Num))){
Num++;
read = fis.read(bufferPacketContent);
buffer=byteMerger(intToByteArray(Num),bufferPacketContent);
packet = new DatagramPacket(buffer,buffer.length,
(Inet4Address) Inet4Address.getByName("localhost"), 8);
packet1=packet;//备份重传
socket.send(packet);
}else{
socket.send(packet1);
}
}
if (read == -1) {
System.out.print("用时:");
System.out.println(new Date().getTime() - a);
break;
}
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (SocketException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}
public static byte[] intToByteArray(int i) {
byte[] result = new byte[4];
//由高位到低位
result[0] = (byte)((i >> 24) & 0xFF);
result[1] = (byte)((i >> 16) & 0xFF);
result[2] = (byte)((i >> 8) & 0xFF);
result[3] = (byte)(i & 0xFF);
return result;
}
public static byte[] byteMerger(byte[] byte_1, byte[] byte_2){
byte[] byte_3 = new byte[byte_1.length+byte_2.length];
System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);
System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);
return byte_3;
}
}
接收端:
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Inet4Address;
import java.net.SocketException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class UdpReceive {
public static void main(String[] args) throws IOException {
new FileRece().Udprecev();;
}
}
class FileRece{
JFrame jFrame;
JPanel InputPanel;
JLabel PortNumberLabel;
JButton buttonSaveFile;
JTextField textPortNumber;
DatagramSocket dataSocket;
final int PORT = 8;
byte[] receiveByte,receiveByte1,sendByte;
DatagramPacket dataPacket;
String FileName,str;
DataOutputStream fileOut;
int j,len;
public FileRece() throws SocketException, FileNotFoundException {
dataSocket = new DatagramSocket(PORT);
fileOut = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(
"D:\\mysql.rar")));
receiveByte = new byte[1080];
sendByte=new byte[20];
}
public void Udprecev() throws IOException{
j=0;
while(true){
dataPacket = new DatagramPacket(receiveByte, receiveByte.length);
dataSocket.receive(dataPacket);
String s=""+j; //把数字转成字符串
len=s.length();
str = new String(dataPacket.getData(),0,len);
System.out.println(str);
if(str.equals(new String().valueOf(j+1))){
// 指定接收到数据的长度,可使接收数据正常显示,开始时很容易忽略这一点
fileOut.write(receiveByte, len, dataPacket.getLength());
fileOut.flush();
j++;
sendByte=intToByteArray(j);
dataPacket = new DatagramPacket(sendByte,sendByte.length,
(Inet4Address) Inet4Address.getByName("localhost"), 9);
dataSocket.send(dataPacket);
}
}
}
public static byte[] intToByteArray(int i) {
byte[] result = new byte[4];
//由高位到低位
result[0] = (byte)((i >> 24) & 0xFF);
result[1] = (byte)((i >> 16) & 0xFF);
result[2] = (byte)((i >> 8) & 0xFF);
result[3] = (byte)(i & 0xFF);
return result;
}
}