




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(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)立一個緩沖區(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,“對方已經(jīng)承受文件傳輸,點(diǎn)擊確定開頭傳輸!“);BufferedInputStream Bin = newFileInputStream(file));//創(chuàng)立一個緩沖區(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個字節(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里面的語句還是會執(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;//讀取對方發(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線程對象用來啟動線程類存才保存下來否存在
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+“已經(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)立一個的緩沖輸出流,以將數(shù)據(jù)寫入指定的輸出流。BufferedInputStreamBin=newBufferedInputStream(socket.getInputStream);//創(chuàng)立一個緩沖區(qū)數(shù)組,socket輸入流,以便使用byte[]by=ne
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 華中師范大學(xué)六年級信息技術(shù)課件
- 海淀中考數(shù)學(xué)試卷
- 鴻鵠志數(shù)學(xué)試卷
- 陜西省商洛市丹鳳中學(xué)2025屆物理高一第二學(xué)期期末教學(xué)質(zhì)量檢測模擬試題含解析
- 2025屆新疆伊西哈拉鎮(zhèn)中學(xué)物理高二下期末統(tǒng)考模擬試題含解析
- 中國家居建材流通行業(yè)市場發(fā)展現(xiàn)狀及投資前景展望報告
- 中國自行車制動器行業(yè)發(fā)展?jié)摿Ψ治黾巴顿Y戰(zhàn)略咨詢報告
- 中國除草劑市場運(yùn)營態(tài)勢分析及投資前景預(yù)測報告
- 鋼材鍛件項目投資可行性研究分析報告(2024-2030版)
- 2021-2026年中國鄭州家裝行業(yè)市場全景調(diào)研及投資規(guī)劃建議報告
- 2025年度地下綜合管廊代建合同模板
- 工程全過程造價咨詢管理及控制要點(diǎn)
- 2025年度藥品區(qū)域代理銷售合同范本3篇
- 輸變電工程監(jiān)督檢查標(biāo)準(zhǔn)化清單-質(zhì)監(jiān)站檢查
- 國家開放大學(xué)法學(xué)本科《商法》期末紙質(zhì)考試第四大題案例分析庫2025珍藏版
- 2024年山東省消防工程查驗(yàn)技能競賽理論考試題庫-下(多選、判斷題)
- 野外活動營地消毒方案
- 安寧療護(hù)的護(hù)理常規(guī)
- 2025年高考英語完形填空+語法填空專練(原卷版+解析版)
- 2025高考數(shù)學(xué)專項復(fù)習(xí):圓錐曲線基礎(chǔ)總結(jié)、二級結(jié)論、方法與技巧
- 安全生產(chǎn)標(biāo)準(zhǔn)化實(shí)施細(xì)則范例(3篇)
評論
0/150
提交評論