




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
发送端/*2011*by小郭*远程文件传输**/importjavax.swing.*;import.*;importjava.io.*;importjava.awt.*;importjava.lang.*;importjava.awt.event.*;publicclassTcpSendextendsJFrameimplementsActionListener{privateJButtonbutton;privateJFileChooserchooser;//privateFileInputStreamin;//privateStringfilename;//byte[]by=newbyte[100000];publicTcpSend{super(“小郭文件传输发送端“);this.setBounds(10,10,400,400);this.setLayout(null);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setResizable(false);this.setVisible(true);chooser=newJFileChooser;button=newJButton(“发送“);button.setFont(newFont(“楷体“,Font.PLAIN,30));button.setBounds(0,0,400,400);add(button);button.addActionListener(this);}publicvoidactionPerformed(ActionEvente){chooser.showOpenDialog(this);if(chooser.showOpenDialog(this)==JFileChooser.CANCEL_OPTION){button.setText(“取消文件发送“);//bug2次才能取消return;}Sendsend=newSend(chooser.getSelectedFile);send.start;button.setText(“文件已发送“);}publicstaticvoidmain(String[]args){newTcpSend;}}//endTcpSendclassSendextendsThread{privateFilefile;privateSocketsocket;privateDataOutputStreamDout;privateDataInputStreamDin;BufferedInputStreambuffered;Send(Filefile){this.file=file;try{socket=newSocket(“localhost“,1111);//localhost可以改成IP假设是内网直接填IPIP要映射buffered=newBufferedInputStream(socket.getInputStream);//创立一个缓冲区数组,s输入流,以便使用Din=newDataInputStream(buffered);//数据输入流,用来读取Dout=newDataOutputStream(socket.getOutputStream);//数据输出流,用来写入由数据输入流读取的数据}catch(IOExceptione){e.printStackTrace;}}publicvoidrun{try{
Dout.writeUTF(file.getName);//将文件名写入输出流JOptionPane.showMessageDialog(null,“发送的文件是:“+file.getName);booleanisAccepted=Din.readBoolean;//接收端是否读取输入字节if(isAccepted){//JOptionPane.showMessageDialog(null,“对方已经承受文件传输,点击确定开头传输!“);BufferedInputStream Bin = newFileInputStream(file));//创立一个缓冲区数组,保存文件输入流byte[]by=newbyte[100000];intl;
BufferedInputStream(newwhile((l=Bin.read(by))!=-1)//by数组中只要不是=-1假设=-1即到达流末尾就跳出循环{出流。
Dout.write(by,0,l);//by01个字节写入输Dout.flush;//清空数据输出流//l=Bin.read(by);多了这一句照成接收的文件大小只有一半的容量}Bin.close;//关闭缓冲输入流//JOptionPane.showMessageDialog(null,file.toString+“\n文件发送完毕!“);}//endif}//endtrycatch(IOExceptione){e.printStackTrace;}finally//保证即使由于特别,tryfinally里面的语句还是会执行,这样可以释放一些资源{try{}
Din.close;//关闭数据输入流Dout.close;//关闭数据输出流socket.close;catch(IOExceptione){e.printStackTrace;}}//endfinally}//endrun}//endSend(线程类)接收端importjavax.swing.*;import.*;importjava.io.*;importjava.awt.*;importjava.lang.*;importjava.awt.event.*;publicclassTcpReceiveextendsJFrameimplementsActionListener{privateJButtonbutton1,button2;privateJLabellabel;privateSocketsocket;privateServerSocketss;privateStringfilename;publicTcpReceive{super(“小郭文件传输接收端“);this.setBounds(420,420,400,400);setLayout(null);setVisible(true);this.setResizable(false);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);label=newJLabel;label.setText(“。“);button1=newJButton(“Accept“);button2=newJButton(“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);button1.addActionListener(this);button2.addActionListener(this);try{ss=newServerSocket(1111);//1111的效劳器套接字while(!ss.isClosed){socket=ss.accept;//侦听并承受到此套接字的连接DataInputStreamDin=newDataInputStream(socket.getInputStream);//数据输入流,1111端口接收到的输入流filename=Din.readUTF;//读取对方发过来的字符串〔即文件名〕label.setText(filename);}}catch(IOExceptione){if(ss.isClosed)//端口关闭就退出{JOptionPane.showMessageDialog(this,“端口已关闭,程序退出“);System.exit(0);}else{e.printStackTrace;}}}//end构造函数publicvoidactionPerformed(ActionEvente){if(e.getSource==button1){JFileChooserchooser=newJFileChooser;chooser.setSelectedFile(newFile(filename));//路径抽象化chooser.showSaveDialog(this);chooser.getName(chooser.getSelectedFile);Receivereceive=newReceive(chooser.getSelectedFile,socket);//Receive线程对象用来启动线程类存才保存下来否存在
if(chooser.showSaveDialog(this)==JFileChooser.APPROVE_OPTION)//bug:2次保{//System.out.println(“bbb“);if(chooser.getSelectedFile.exists)//测试此抽象路径名表示的文件或名目是{int over=JOptionPane.showConfirmDialog(this,“ 文 件“+chooser.getSelectedFile.getName+“已经存在,确定掩盖吗?掩盖与否“,JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);if(over==JOptionPane.YES_OPTION)//bug:2次确定才能执行{//else{System.out.println(“aaa“);}receive.start;}//endif
}//endifelse{receive.start;}//System.out.println(“aaaa“);}//bug:2次取消才能取消其次次取消才打印}//endif}//endifif(e.getSource==button2){if(label.getText==“。“){}else{}
JOptionPane.showMessageDialog(this,“没收到恳求哦!“);JOptionPane.showMessageDialog(this,“正在退出!“);System.exit(0);}//endif}//endactionPerformedpublicstaticvoidmain(Stringargs[]){newTcpReceive;}}//endTcpReceiveclassReceiveextendsThread{privateFilefile;privateSocketsocket;intl;publicReceive(Filefile,Socketsocket){this.file=file;this.socket=socket;}publicFilelocation{returnfile;}publicvoidrun{if(file==null){}else{
JOptionPane.showMessageDialog(null,“没有监听到文件“);return;try{DataOutputStream Dout = newDataOutputStream(socket.getOutputStream);//数据输出流,用来写入由数据输入流读取的数据Dout.writeBoolean(true);}catch(IOExceptione){e.printStackTrace;}}//endelsetry{FileOutputStreamfout=newFileOutputStream(file);//file表示的文件中写入数据的文件输出流。即上面的chooser.getSelectedFileBufferedOutputStreamBout=newBufferedOutputStream(fout);//创立一个的缓冲输出流,以将数据写入指定的输出流。BufferedInputStreamBin=newBufferedInputStream(socket.getInputStream);//创立一个缓冲区数组,socket输入流,以便使用byte[]by=ne
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 货物运输包车合同
- 志愿者清洗家园活动方案
- 徒步邕江活动方案
- 开园活动拍照活动方案
- 开学班集体活动方案
- 往届书画活动方案
- 微信线上营销活动方案
- 弘扬英雄活动方案
- 开公司团建活动策划方案
- 开工剪彩活动方案
- 水泥搅拌桩施工项目进度管理措施
- 人工智能赋能教师数字素养提升
- 换电站合同范例
- 【超星学习通】马克思主义基本原理(南开大学)尔雅章节测试网课答案
- (CNAS-CL01-2018认可准则)内审核查表
- 2024年模型模具加工合同范本
- 广东省高州市2023-2024学年高一下学期期中考试数学
- 食堂工作人员考核方案
- 国家基本公卫(老年人健康管理)考试复习题及答案
- 临床营养(043)(正高级)高级卫生专业技术资格考试试卷及答案指导(2025年)
- 重庆市旋挖成孔灌注桩工程技术规程
评论
0/150
提交评论