import java.awt.*;
import java.awt.event.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class transfer extends WindowAdapter implements ActionListener
{
public static Frame frame;
public static Button button1, button2, button3, button4, button5, button6;
public static Label label1, label2, label3;
public static TextArea textarea1, textarea2, textarea3;
public static TextField textfield;
public static String name, info, ip, name2, info2;
Dialog dialog1, dialog2;
public static int passedlen = 0;
public static long len = 0;
public static boolean flag1 = true;
public static boolean flag2 = true;
public static boolean flag3 = true;
public static JProgressBar pro;
public void transfer()// interface
{
frame = new Frame("File Transfer");
frame.setSize(400, 300);
int height = 300;
int width = 400;
Toolkit lc = Toolkit.getDefaultToolkit();
frame.setLocation((lc.getScreenSize().width - width) / 2,
(lc.getScreenSize().height - height) / 2);
frame.setLayout(new GridBagLayout()); // use grid
frame.setResizable(false);
dialog1 = new Dialog(frame, "Error", true);
dialog2 = new Dialog(frame, "Instruction", true);
// build component
label1 = new Label("client IP Address:");
label2 = new Label("illegal IP Address!");
textarea1 = new TextArea(1, 35);
textarea1.setEditable(false);
textarea2 = new TextArea(5, 45);
textarea2.setEditable(false);
textarea3 = new TextArea(2, 30);
textarea3.setEditable(false);
textarea3.append("Server:\n1.Select a file\n2.press Send\n3.wait for the client\n\nClient:\n1.when sending begun\n2.input ip of Server\n2.press Recevie");
// textarea1.setLineWrap(true);
textfield = new TextField(13);
button1 = new Button("Select File");
button2 = new Button("Send");
button3 = new Button("Receive");
button4 = new Button("Save As");
button5 = new Button("Refuse");
button6 = new Button("Instruction");
pro = new JProgressBar();
transfer.pro.setMaximum(100);
transfer.pro.setMinimum(0);
transfer.pro.setStringPainted(true);
// define property of grid
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH; // extendable on all sides
c.insets = new Insets(5, 5, 5, 5); // 5 pixels interval on all sides
// location
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
frame.add(button6, c);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
frame.add(button1, c);
c.gridx = 1;
c.gridy = 0;
c.gridwidth = 2;
c.gridheight = 2;
frame.add(textarea1, c);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
c.gridheight = 1;
frame.add(label1, c);
c.gridx = 1;
c.gridy = 2;
c.gridwidth = 1;
c.gridheight = 1;
frame.add(textfield, c);
c.gridx = 2;
c.gridy = 2;
c.gridwidth = 1;
c.gridheight = 1;
frame.add(button2, c);
c.gridx = 0;
c.gridy = 3;
c.gridwidth = 3;
c.gridheight = 1;
frame.add(textarea2, c);
c.gridx = 0;
c.gridy = 5;
c.gridwidth = 1;
c.gridheight = 1;
frame.add(button3, c);
c.gridx = 1;
c.gridy = 5;
c.gridwidth = 1;
c.gridheight = 1;
frame.add(button4, c);
c.gridx = 2;
c.gridy = 5;
c.gridwidth = 1;
c.gridheight = 1;
frame.add(button5, c);
c.gridx = 0;
c.gridy = 4;
c.gridwidth = 3;
c.gridheight = 1;
frame.add(pro, c);
// component listened
frame.addWindowListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
dialog1.addWindowListener(this);
dialog2.addWindowListener(this);
frame.setVisible(true);
}
public boolean ipcheck(String ip)
{
ip = textfield.getText();
String num = "((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))";
Pattern pattern = Pattern.compile(num);
Matcher matcher = pattern.matcher(ip);
return matcher.matches();
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == button1)
{
FileDialog frame1 = new FileDialog(frame, "Select File",
FileDialog.LOAD);
frame1.show();
name = frame1.getFile();
info = frame1.getDirectory() + frame1.getFile();
File fi = new File(info);
if (name != null)// display file's name & size
{
textarea1.append("Name:" + fi.getName() + "\nSize:"
+ (int) fi.length() + "Bytes\n");
textarea2.append("prepare to send.\n");
}
}
else if (e.getSource() == button2)
{
if (info == null)
{
textarea2.append("no file selected\n");
} else
{
textarea2.append("waiting for client...\n");
send s = new send();
s.start();
}
}
else if (e.getSource() == button3)
{
// ip is legal or not
if (!ipcheck(ip))
{
dialog1.add(label2);
dialog1.setSize(200, 100);
Toolkit d = Toolkit.getDefaultToolkit();
dialog1.setLocation((d.getScreenSize().width - 200) / 2,
(d.getScreenSize().height - 100) / 2);
dialog1.setVisible(true);
} else
{
flag3 = true;
receive r = new receive();
r.start();
}
}
else if (e.getSource() == button4)
{
if (!ipcheck(ip))
{
dialog1.add(label2);
dialog1.setSize(200, 100);
Toolkit d = Toolkit.getDefaultToolkit();
dialog1.setLocation((d.getScreenSize().width - 200) / 2,
(d.getScreenSize().height - 100) / 2);
dialog1.setVisible(true);
} else
{
FileDialog frame2 = new FileDialog(frame, "Save As",
FileDialog.SAVE);
frame2.show();
name2 = frame2.getFile();
info2 = frame2.getDirectory() + frame2.getFile();
// textarea2.append(info2 +"\n");
flag3 = false;
receive r = new receive();
r.start();
}
}
else if (e.getSource() == button5)
{
textarea2.append("receive canceled.\n");
}
else if (e.getSource() == button6)
{
dialog2.add(textarea3);
dialog2.setSize(240, 180);
Toolkit d = Toolkit.getDefaultToolkit();
dialog2.setLocation((d.getScreenSize().width - 240) / 2,
(d.getScreenSize().height - 180) / 2);
dialog2.setVisible(true);
}
}
public void windowClosing(WindowEvent e)
{
if (e.getSource() == frame)
{
System.exit(0);
}
else if (e.getSource() == dialog1)
{
dialog1.setVisible(false);
dialog1.dispose();
}
else if (e.getSource() == dialog2)
{
dialog2.setVisible(false);
dialog2.dispose();
}
}
public static void main(String args[])
{
transfer start = new transfer();
start.transfer();
}
}
class send extends Thread
{
private int port = 8889;
public void start()
{
Socket s = null;
try
{
ServerSocket ss = new ServerSocket(port);
while (transfer.flag2)
{
transfer.textarea2.append("begin send\n");
File fi = new File(transfer.info);
s = ss.accept();// listening, block before connection
DataInputStream dis = new DataInputStream(
new BufferedInputStream(s.getInputStream()));
dis.readByte();
DataInputStream fis = new DataInputStream(
new BufferedInputStream(new FileInputStream(
transfer.info)));
DataOutputStream ps = new DataOutputStream(s.getOutputStream());
// send name & size
ps.writeUTF(fi.getName());
ps.flush();
ps.writeLong((long) fi.length());
ps.flush();
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
while (true)
{
int read = 0;
if (fis != null)
{
read = fis.read(buf);
}
if (read == -1)
{
break;
}
p
java基于socket的文件发送与接收,有界面,有进度条
3星 · 超过75%的资源 需积分: 50 53 浏览量
2013-06-24
22:43:26
上传
评论 2
收藏 13KB RAR 举报
aqurius0725
- 粉丝: 0
- 资源: 7
- 1
- 2
- 3
前往页