




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
發(fā)送端/*2011*by小郭*遠(yuǎn)程文件傳輸**/importjavax.swing.*;import.*;importjava.io.*;importjava.awt.*;importjava.lang.*;importjava.awt.event.*;publicclassTcpSendextendsJFrameimplementsActionListener{privateJButtonbutton;privateJFileChooserchooser;//privateFileInputStreamin;//privateStringfilename;//byte[]by=newbyte[100000];publicTcpSend{super(“小郭文件傳輸發(fā)送端“);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(“發(fā)送“);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(“取消文件發(fā)送“);//bug2次才能取消return;}Sendsend=newSend(chooser.getSelectedFile);send.start;button.setText(“文件已發(fā)送“);}publicstaticvoidmain(String[]args){newTcpSend;}}//endTcpSendclassSendextendsThread{privateFilefile;privateSocketsocket;privateDataOutputStreamDout;privateDataInputStreamDin;BufferedInputStreambuffered;Send(Filefile){this.file=file;try{socket=newSocket(“l(fā)ocalhost“,1111);//localhost可以改成IP假設(shè)是內(nèi)網(wǎng)直接填I(lǐng)PIP要映射buffered=newBufferedInputStream(socket.getInputStream);//創(chuàng)立一個(gè)緩沖區(qū)數(shù)組,s輸入流,以便使用Din=newDataInputStream(buffered);//數(shù)據(jù)輸入流,用來讀取Dout=newDataOutputStream(socket.getOutputStream);//數(shù)據(jù)輸出流,用來寫入由數(shù)據(jù)輸入流讀取的數(shù)據(jù)}catch(IOExceptione){e.printStackTrace;}}publicvoidrun{try{
Dout.writeUTF(file.getName);//將文件名寫入輸出流JOptionPane.showMessageDialog(null,“發(fā)送的文件是:“+file.getName);booleanisAccepted=Din.readBoolean;//接收端是否讀取輸入字節(jié)if(isAccepted){//JOptionPane.showMessageDialog(null,“對(duì)方已經(jīng)承受文件傳輸,點(diǎn)擊確定開頭傳輸!“);BufferedInputStream Bin = newFileInputStream(file));//創(chuàng)立一個(gè)緩沖區(qū)數(shù)組,保存文件輸入流byte[]by=newbyte[100000];intl;
BufferedInputStream(newwhile((l=Bin.read(by))!=-1)//by數(shù)組中只要不是=-1假設(shè)=-1即到達(dá)流末尾就跳出循環(huán){出流。
Dout.write(by,0,l);//by01個(gè)字節(jié)寫入輸Dout.flush;//清空數(shù)據(jù)輸出流//l=Bin.read(by);多了這一句照成接收的文件大小只有一半的容量}Bin.close;//關(guān)閉緩沖輸入流//JOptionPane.showMessageDialog(null,file.toString+“\n文件發(fā)送完畢!“);}//endif}//endtrycatch(IOExceptione){e.printStackTrace;}finally//保證即使由于特別,tryfinally里面的語句還是會(huì)執(zhí)行,這樣可以釋放一些資源{try{}
Din.close;//關(guān)閉數(shù)據(jù)輸入流Dout.close;//關(guān)閉數(shù)據(jù)輸出流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);//數(shù)據(jù)輸入流,1111端口接收到的輸入流filename=Din.readUTF;//讀取對(duì)方發(fā)過來的字符串〔即文件名〕label.setText(filename);}}catch(IOExceptione){if(ss.isClosed)//端口關(guān)閉就退出{JOptionPane.showMessageDialog(this,“端口已關(guān)閉,程序退出“);System.exit(0);}else{e.printStackTrace;}}}//end構(gòu)造函數(shù)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線程對(duì)象用來啟動(dòng)線程類存才保存下來否存在
if(chooser.showSaveDialog(this)==JFileChooser.APPROVE_OPTION)//bug:2次保{//System.out.println(“bbb“);if(chooser.getSelectedFile.exists)//測(cè)試此抽象路徑名表示的文件或名目是{int over=JOptionPane.showConfirmDialog(this,“ 文 件“+chooser.getSelectedFile.getName+“已經(jīng)存在,確定掩蓋嗎?掩蓋與否“,JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);if(over==JOptionPane.YES_OPTION)//bug:2次確定才能執(zhí)行{//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,“沒有監(jiān)聽到文件“);return;try{DataOutputStream Dout = newDataOutputStream(socket.getOutputStream);//數(shù)據(jù)輸出流,用來寫入由數(shù)據(jù)輸入流讀取的數(shù)據(jù)Dout.writeBoolean(true);}catch(IOExceptione){e.printStackTrace;}}//endelsetry{FileOutputStreamfout=newFileOutputStream(file);//file表示的文件中寫入數(shù)據(jù)的文件輸出流。即上面的chooser.getSelectedFileBufferedOutputStreamBout=newBufferedOutputStream(fout);//創(chuàng)立一個(gè)的緩沖輸出流,以將數(shù)據(jù)寫入指定的輸出流。BufferedInputStreamBin=newBufferedInputStream(socket.getInputStream);//創(chuàng)立一個(gè)緩沖區(qū)數(shù)組,socket輸入流,以便使用byte[]by=ne
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- T/CBJ 6101-2024紹興東路酒生產(chǎn)技術(shù)規(guī)范
- T/CATCM 020-2023龜甲膠質(zhì)量規(guī)范
- T/CAQI 12-2016家用和類似用途飲用水處理裝置用管接件
- T/CANSI 6-2019船用中空螺旋槳
- 阿亮java面試題及答案
- 河池衛(wèi)校面試題及答案
- 高升??荚囶}及答案
- 人類健康課件
- 販賣會(huì)考試題及答案
- 產(chǎn)品面試題模板及答案
- 2023版煤礦安全管理人員考試題庫及解析
- 2025年標(biāo)準(zhǔn)育兒嫂合同樣本
- 互聯(lián)網(wǎng)金融(同濟(jì)大學(xué))知到智慧樹章節(jié)測(cè)試課后答案2024年秋同濟(jì)大學(xué)
- 2025年江蘇鹽城市燕舞集團(tuán)有限公司招聘筆試參考題庫含答案解析
- 整體施工勞務(wù)服務(wù)方案
- 黃金管理制度
- 2025年貴州盤江精煤股份有限公司招聘筆試參考題庫含答案解析
- 2025年上半年陜西西安市事業(yè)單位招聘高層次及緊缺特殊專業(yè)人才690人重點(diǎn)基礎(chǔ)提升(共500題)附帶答案詳解-1
- 2025年四川涼山道德與法制中考試卷
- 江蘇省淮陰區(qū)2025屆高三下第一次測(cè)試數(shù)學(xué)試題含解析
- 人工智能賦能教師數(shù)字素養(yǎng)提升
評(píng)論
0/150
提交評(píng)論