ImageVerifierCode 换一换
格式:DOCX , 页数:17 ,大小:145.03KB ,
资源ID:5111435      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/5111435.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(java文件网络传输.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

java文件网络传输.docx

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