Java远程传输文件(增加覆盖取消等功能)_第1页
Java远程传输文件(增加覆盖取消等功能)_第2页
Java远程传输文件(增加覆盖取消等功能)_第3页
Java远程传输文件(增加覆盖取消等功能)_第4页
Java远程传输文件(增加覆盖取消等功能)_第5页
已阅读5页,还剩8页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、发送端/ *by小郭 *远程文件传输 */import javax.swing.*;import .*;import java.io.*;import java.awt.*;import java.lang.*;import java.awt.event.*;public class TcpSend extends JFrame implements ActionListenerprivate JButton button; private JFileChooser chooser; /private FileInputStream in;/private String filename;/b

2、yte by=new byte100000;public TcpSend()super("小郭文件传输发送端");this.setBounds(10,10,400,400);this.setLayout(null);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setResizable(false);this.setVisible(true);chooser=new JFileChooser();button=new JButton("发送");button.setFont(new Fo

3、nt("楷体",Font.PLAIN,30);button.setBounds(0,0,400,400);add(button);button.addActionListener(this); public void actionPerformed(ActionEvent e) chooser.showOpenDialog(this); if(chooser.showOpenDialog(this)=JFileChooser.CANCEL_OPTION) button.setText("取消文件发送");/小bug 按2次才能取消 return; Sen

4、d send=new Send(chooser.getSelectedFile();send.start();button.setText("文件已发送");public static void main(String args)new TcpSend();/end TcpSendclass Send extends Threadprivate File file; private Socket socket; private DataOutputStream Dout; private DataInputStream Din; BufferedInputStream bu

5、ffered; Send(File file) this.file = file; try socket= new Socket("localhost",1111);/localhost可以改成IP 如果是内网 直接填内网IP 外网的话IP要映射 buffered=new BufferedInputStream(socket.getInputStream();/创建一个缓冲区数组,保存套接字s输入流,以便使用 Din = new DataInputStream(buffered);/数据输入流,用来读取 Dout = new DataOutputStream(socket.

6、getOutputStream();/数据输出流,用来写入由数据输入流读取的数据 catch (IOException e) e.printStackTrace(); public void run() try Dout.writeUTF(file.getName();/将文件名写入输出流 JOptionPane.showMessageDialog(null,"发送的文件是:"+file.getName(); boolean isAccepted = Din.readBoolean();/接收端是否读取输入字节 if(isAccepted) / JOptionPane.sh

7、owMessageDialog(null,"对方已经接受文件传输,点击确定开始传输!"); BufferedInputStream Bin = new BufferedInputStream(new FileInputStream(file);/创建一个缓冲区数组,保存文件输入流 byte by = new byte100000; int l; while( l =Bin.read(by)!= -1)/从输入流中将各字节读取到by数组中 只要不是=-1 如果=-1即到达流末尾就跳出循环 Dout.write(by,0,l);/将by数组中从偏移地址0开始的1个字节写入输出流

8、。 Dout.flush();/清空数据输出流 /l = Bin.read(by);多了这一句 照成接收的文件大小只有一半的容量 Bin.close();/关闭缓冲输入流 / JOptionPane.showMessageDialog(null,file.toString()+"n文件发送完毕!"); /end if /end try catch (IOException e) e.printStackTrace(); finally/保证即使因为异常,try里面的代码不会被执行,但是finally里面的语句还是会执行,这样可以释放一些资源 try Din.close();

9、/关闭数据输入流 Dout.close();/关闭数据输出流 socket.close(); catch (IOException e) e.printStackTrace(); /end finally /end run()/end Send(线程类)*2011接收端import javax.swing.*;import .*;import java.io.*;import java.awt.*;import java.lang.*;import java.awt.event.*;public class TcpReceive extends JFrame implements Action

10、Listenerprivate JButton button1,button2; private JLabel label; private Socket socket; private ServerSocket ss; private String filename; public TcpReceive()super("小郭文件传输接收端");this.setBounds(420,420,400,400);setLayout(null);setVisible(true);this.setResizable(false);this.setDefaultCloseOperat

11、ion(JFrame.EXIT_ON_CLOSE);label=new JLabel();label.setText("暂时没收到文件请求,请等待。");button1=new JButton("Accept");button2=new JButton("Cancel");add(label);add(button1);add(button2);label.setBounds(10,10,300,300);button1.setBounds(60,310,100,30);button2.setBounds(240,310,100,30

12、);button1.addActionListener(this);button2.addActionListener(this);try ss=new ServerSocket(1111);/绑定到端口1111的服务器套接字 while(!ss.isClosed() socket=ss.accept();/侦听并接受到此套接字的连接 DataInputStream Din = new DataInputStream(socket.getInputStream();/数据输入流,用来读取从1111端口接收到的输入流 filename = Din.readUTF();/读取对方发过来的字符串(即

13、文件名) label.setText(filename); catch (IOException e) if(ss.isClosed()/端口关闭就退出 JOptionPane.showMessageDialog(this,"端口已关闭,程序退出"); System.exit(0); else e.printStackTrace(); /end构造函数public void actionPerformed(ActionEvent e) if(e.getSource()=button1) JFileChooser chooser=new JFileChooser(); cho

14、oser.setSelectedFile(new File(filename);/路径抽象化 chooser.showSaveDialog(this); chooser.getName(chooser.getSelectedFile(); Receive receive = new Receive(chooser.getSelectedFile(),socket);/创建Receive线程对象 用来启动线程类 if(chooser.showSaveDialog(this)=JFileChooser.APPROVE_OPTION)/bug:按2次保存才保存下来 /System.out.print

15、ln("bbb"); if(chooser.getSelectedFile().exists()/测试此抽象路径名表示的文件或目录是否存在 int over=JOptionPane.showConfirmDialog(this,"文件"+chooser.getSelectedFile().getName()+"已经存在,确定覆盖吗?","覆盖与否", JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE); if(over=JOptionPane.YES_OPT

16、ION)/bug:按2次确定才能执行 /elseSystem.out.println("aaa"); receive.start(); /end if /end if elsereceive.start(); /System.out.println("aaaa"); /bug:按2次取消才能取消 第二次取消才打印 /end if /end if if(e.getSource()=button2) if(label.getText()="暂时没收到文件请求,请等待。") JOptionPane.showMessageDialog(thi

17、s,"没收到请求哦!"); else JOptionPane.showMessageDialog(this,"正在退出!"); System.exit(0); /end if/end actionPerformedpublic static void main(String args)new TcpReceive();/end TcpReceiveclass Receive extends Thread private File file; private Socket socket; int l; public Receive(File file, S

18、ocket socket) this.file=file; this.socket=socket; public File location() return file; public void run() if(file = null) JOptionPane.showMessageDialog(null,"没有监听到文件"); return; else try DataOutputStream Dout = new DataOutputStream(socket.getOutputStream();/数据输出流,用来写入由数据输入流读取的数据 Dout.writeBoo

19、lean(true); catch (IOException e) e.printStackTrace(); /end else try FileOutputStream fout = new FileOutputStream(file);/创建一个向指定file表示的文件中写入数据的文件输出流。即上面的chooser.getSelectedFile() BufferedOutputStream Bout = new BufferedOutputStream(fout);/创建一个新的缓冲输出流,以将数据写入指定的输出流。 BufferedInputStream Bin = new BufferedInputStream(socket.getInputStream();/创建一个缓冲区数组,保存套接字socket输入流,以便使用 byte by = new byte100000; int l ; int yes=J

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论