java文件网络传输
- 格式:docx
- 大小:209.92 KB
- 文档页数:13
软件界面: 软件初始界面
由用户自行输入要链接的ip地址,这里我测试的是自己链接自己的ip地址 用户选择自己要发送的文件
显示对方要向我发送文件,提示用户接收文件 用户选择接收文件存放位置
接收完成,自动断开链接 软件简要:
源码分为1个包(包名为:文件传输),4个java文件(zy.java、ddjs.java、send.java、js.java),zy.java为主界面,ddjs.java用于接收请求连接,send.java用于发送文件,zy.java用于接收文件。
zy.java: package 文件传输; import java.awt.Frame; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException;
import javax.swing.DefaultListModel; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField;
public class zy extends Frame implements ActionListener{ public static JList yonghu=null; public static DefaultListModel mx; private JButton lianjie,send,rece; public static JLabel now,zdy; public static int end=0; public Socket a = null; public String h; public static Socket client=null; public static File file; public static int length = 0; public static byte[] sendByte = null; public static Socket socket = null; public static DataOutputStream dout = null; public static FileInputStream fin = null; public static ServerSocket server=null; public static int p=0; public static ddjs as=new ddjs(); public JTextField ip; public zy() throws IOException { super("文件传输 1.0 By:若离随风"); try { server=new ServerSocket(12394); } catch (IOException e1) { System.out.println("端口建立失败"); JOptionPane.showMessageDialog(null, "开放连接失败,端口可能被其他软件占用", "错误提示", JOptionPane.ERROR_MESSAGE); } as.start(); ImageIcon img = new ImageIcon("img\\028.png"); ImageIcon rec = new ImageIcon("img\\029.png"); ip=new JTextField(); setNow(new JLabel("当前暂无连接")); zdy=new JLabel("请输入对方ip:"); lianjie=new JButton("建立连接"); send=new JButton(img); rece=new JButton(rec); mx=new DefaultListModel(); yonghu=new JList(mx); yonghu.setEnabled(false); yonghu.setVisibleRowCount(20); JScrollPane jsp = new JScrollPane(yonghu); lianjie.setBounds(new Rectangle(630,38,120,25)); jsp.setBounds(new Rectangle(20,70,730,400)); getNow().setBounds(new Rectangle(20,30,400,40)); send.setBounds(new Rectangle(20,500,40,40)); rece.setBounds(new Rectangle(80,500,40,40)); zdy.setBounds(new Rectangle(390,30,400,40)); ip.setBounds(new Rectangle(470,38,150,25)); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); } public void windowClosed(WindowEvent e) { try { server.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); lianjie.addActionListener(this); send.addActionListener(this); rece.addActionListener(this); this.add(jsp); this.add(lianjie); this.add(getNow()); this.add(send); this.add(rece); this.add(zdy); this.add(ip); this.setLayout(null); this.setSize(800,580); this.setLocation(600,150); this.setVisible(true); } public void actionPerformed(ActionEvent ev) { if(ev.getSource()==lianjie) { h=ip.getText(); if(h.equals("")) JOptionPane.showMessageDialog(null, "请输入ip地址", "错误提示", JOptionPane.ERROR_MESSAGE); else { try { socket=new Socket(h,12394); } catch (UnknownHostException e) { JOptionPane.showMessageDialog(null, "非法的ip地址", "错误提示", JOptionPane.ERROR_MESSAGE); } catch (IOException e) { JOptionPane.showMessageDialog(null, "链接失败,对方可能未开启软件", "错误提示", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); }
if(socket.isClosed() == false && socket.isConnected() == true) {
now.setText("当前连接ip:"+String.valueOf(yonghu.getSelectedValue()));
} }
} if(ev.getSource()==send) { file=null; JFileChooser jfc=new JFileChooser(); jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES ); jfc.showDialog(new JLabel(), "选择"); file=jfc.getSelectedFile(); new send(client,file).start();
} if(ev.getSource()==rece) { File rec=null; JFileChooser jfc=new JFileChooser(); jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY ); jfc.showDialog(new JLabel(), "选择"); rec=jfc.getSelectedFile(); new js(socket,rec).start(); }
} public static void main(String args[]) throws IOException { new zy(); } public static void jiance() {