1、java文件网络传输软件界面:软件初始界面由用户自行输入要链接的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.R
2、ectangle;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 .S
3、erverSocket;import .Socket;import .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
4、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 Str
5、ing 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
6、 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_MESS
7、AGE); as.start(); ImageIcon img = new ImageIcon(img028.png); ImageIcon rec = new ImageIcon(img029.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);
8、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(n
9、ew 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
10、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,58
11、0); 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
12、) 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.val
13、ueOf(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; J
14、FileChooser 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() DataInputStream in=null
15、; try in=new DataInputStream(zy.socket.getInputStream(); catch (IOException e1) try if(in.readUTF().equals(send) mx.addElement(对方请求向你发送文件,若接收请点击接收按钮); catch (IOException e) static JLabel getNow() return now; public static void setNow(JLabel now) zy.now = now; ddjs.java:package 文件传输;import java.io.IO
16、Exception;import .ServerSocket;import .Socket;import javax.swing.JOptionPane;public class ddjs extends Thread public void run() while(true) try zy.client=zy.server.accept(); catch (IOException e) if(zy.client.isClosed()=false) JOptionPane.showMessageDialog(null, 已经与+zy.client.getInetAddress()+建立连接);
17、 zy.now.setText(当前连接ip:+zy.client.getInetAddress(); zy.jiance(); public ddjs() public static void main(String ag) send.javapackage 文件传输;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.i
18、o.IOException;import .ServerSocket;import .Socket;import javax.swing.JOptionPane;public class send extends Thread private File file; public void run() int length = 0; int a=0; byte sendByte = null; Socket socket = null; DataOutputStream dout = null; DataInputStream in=null; FileInputStream fin = nul
19、l; try dout = new DataOutputStream(zy.client.getOutputStream(); catch (IOException e2) try fin = new FileInputStream(file); catch (FileNotFoundException e2) try in=new DataInputStream(zy.client.getInputStream(); catch (IOException e1) try dout.writeUTF(send); catch (IOException e2) String s=null; wh
20、ile(a=0) try s=in.readUTF(); catch (IOException e1) System.out.println(s); if(s.equals(OK)=true) a=1; try try sendByte = new byte1024; dout.writeUTF(file.getName(); while(length = fin.read(sendByte, 0, sendByte.length)0) dout.write(sendByte,0,length); dout.flush(); catch (Exception e) finally if (do
21、ut != null) dout.close(); if (fin != null) fin.close(); if (socket != null) zy.client.close(); / zy.server.close(); catch (Exception e) e.printStackTrace(); public send(Socket a,File file) this.file=file; public static void main(String s) js.java:package 文件传输;import java.io.DataInputStream;import ja
22、va.io.DataOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import .ServerSocket;import .Socket;import .UnknownHostException;import javax.swing.JOptionPane;public class js extends Thread private Socket socket; private File file; public void run() byte inputB
23、yte = null; int length = 0; DataInputStream din = null; DataOutputStream o=null; FileOutputStream fout = null; try o=new DataOutputStream(zy.socket.getOutputStream(); catch (IOException e1) / TODO Auto-generated catch block e1.printStackTrace(); try o.writeUTF(OK); catch (IOException e1) try din = n
24、ew DataInputStream(zy.socket.getInputStream(); fout = new FileOutputStream(new File(file.getCanonicalPath()+din.readUTF(); inputByte = new byte1024; System.out.println(开始接收数据.); while (true) if (din != null) length = din.read(inputByte, 0, inputByte.length); if (length = -1) break ; System.out.print
25、ln(length); fout.write(inputByte, 0, length); fout.flush(); System.out.println(完成接收); JOptionPane.showMessageDialog(null, 文件接收完成,链接已断开); zy.now.setText(当前暂无链接); catch (Exception ex) ex.printStackTrace(); finally if (fout != null) try fout.close(); catch (IOException e) / TODO Auto-generated catch bl
26、ock e.printStackTrace(); if (din != null) try din.close(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); if (zy.socket != null) try zy.socket.close(); / zy.server.close(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); public js(Socket a,File file) this.socket=a; this.file=file; public static void main(String s) 416353539
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1