Java遠(yuǎn)程傳輸文件(增加覆蓋取消等功能)_第1頁
Java遠(yuǎn)程傳輸文件(增加覆蓋取消等功能)_第2頁
Java遠(yuǎn)程傳輸文件(增加覆蓋取消等功能)_第3頁
Java遠(yuǎn)程傳輸文件(增加覆蓋取消等功能)_第4頁
Java遠(yuǎn)程傳輸文件(增加覆蓋取消等功能)_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(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,“對方已經(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;//讀取對方發(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線程對象用來啟動(dòng)線程類存才保存下來否存在

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)立一個(gè)的緩沖輸出流,以將數(shù)據(jù)寫入指定的輸出流。BufferedInputStreamBin=newBufferedInputStream(socket.getInputStream);//創(chuàng)立一個(gè)緩沖區(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)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論