java網(wǎng)絡文件傳輸?shù)膶崿F(xiàn)_第1頁
java網(wǎng)絡文件傳輸?shù)膶崿F(xiàn)_第2頁
java網(wǎng)絡文件傳輸?shù)膶崿F(xiàn)_第3頁
java網(wǎng)絡文件傳輸?shù)膶崿F(xiàn)_第4頁
java網(wǎng)絡文件傳輸?shù)膶崿F(xiàn)_第5頁
免費預覽已結(jié)束,剩余5頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

1、AllRightsReserved!本程序分為服務器和客戶端兩個小程序。主要實現(xiàn)的功能是:客戶端向服務器端請求一個文件的傳輸連接,服務器接收到請求命令后,要求客戶端發(fā)送用戶名和密碼,如果正確,就執(zhí)行傳輸文件的過程,客戶端接收完后,保存到一個文件中并在屏幕上顯示出來。設計原理:服務器端建立一個SocketServer等待客戶端的連接,每來一個客戶端的請求連接, 就創(chuàng)建一個新的線程對其進行單獨服務,然后繼續(xù)等待其他的客戶端的連接??蛻舳讼蚍掌靼l(fā)送文件傳輸請求,在服務器端要求鑒權(quán)時,輸入用戶名和密碼發(fā)送給服務器驗證,如果驗證通過就開始文件傳輸。使用方法,如果需要重新編譯的話輸入以下兩條命令:jav

2、acSendFileSocket.javajavacSendFileClient.java在命令行下的運行方式:服務器端:javaSendFileSocket客戶端:javaSendFileClientserverIPAddress例如:javaSendFileClient53服務器程序:/packagezieckey.socket;.*;importjava.io.*;/*一個簡單的多線程服務器程序,用于傳輸文件*authorzieckeyjava 網(wǎng)絡文件傳輸?shù)膶崿F(xiàn)Socket 編程*/publicclassSendFileSocketextendsThread(/*

3、paramargs*/publicstaticvoidmain(Stringargs)(/*if(args.length0)/如果有參數(shù)輸入就啟動服務器程序(server();else(/否則啟動客戶端進程client();*/server();/啟動服務器程序privatestaticfinalintPORT=6000;privateSockets;privatestaticfinalStringname=zieckeyprivatestaticfinalStringpassword=123456publicSendFileSocket (Sockets)(this.s=s;publicvo

4、idrun()(try(Outputstreamos=s.getOutputStream();Inputstreamis=s.getInputStream();os.write(Hello,welcomeyou!.getBytes();byte口buf=newbyte100;while(true)(intlen=is.read(buf);StringrevStr=newString(buf,0,len);System.out.println(Thisclientwantsto+revStr);StringfileName;if(revStr.startsWith(get)/表明客戶端請求傳輸一

5、個文件(os.write(Pleaseinputyournameandpassword!Usingtheformat:namepassword.getBytes();fileName=getFileName(revStr);len=is.read(buf);revStr=newString(buf,0,len);System.out.println(Thereceivedusernameandpassword:+revStr);if(revStr.startsWith(zieckey123456)(FileInputStreamfins=newFileInputStream(fileName)

6、;/bytefielBuf=newbyte100;intdata;while(-1!=(data=fins.read()/從文件中讀取數(shù)據(jù),每次讀取1字節(jié)os.write(data);/將讀取到的數(shù)據(jù)寫到網(wǎng)絡數(shù)據(jù)流中發(fā)送給客戶段break;elseos.write(getingfilesusageis:getfilename.getBytes();os.close();is.close();s.close();catch(Exceptione)e.printStackTrace();/*作用:從客戶端發(fā)來了文件請求命令中提取出所請求的文件名*參數(shù):客戶端發(fā)來了文件請求命令字符串,應該以“get

7、”開頭*返回值:提取出所請求的文件名*/privateStringgetFileName(StringrevStr)(StringfileName;fileName=revStr.substring(3);while(fileName.startsWith()(fileName=fileName.substring(1);returnfileName;publicstaticvoidserver()(System.out.println(Thisisserver);try(ServerSocketss=newServerSocket(PORT);intcount=0;while(true)(/

8、創(chuàng)建一個Socket等待客戶端連接Sockets=ss.accept();count+;newSendFileSocket(s).start();/啟動一個線程為這個客戶端服務catch(Exceptionex)ex.printStackTrace();/*publicstaticvoidclient()System.out.println(Thisisclient);try/創(chuàng)建一個SocketSockets=newSocket(InetAddress.getByName(null),PORT);OutputStreamos=s.getOutputStream();/輸出流InputStre

9、amis=s.getInputStream();/輸入流bytebuf=newbyte100;intlen=is.read(buf);/從輸入流中讀取數(shù)據(jù)到bufSystem.out.println(newString(buf,0,len);/向輸出流中寫入數(shù)據(jù),請求傳輸一個文件os.write(getserver.txt.getBytes();len=is.read(buf);/從輸入流中讀取數(shù)據(jù)到bufStringtempStr=newString(buf,0,len);if(tempStr.startsWith(Pleaseinputyournameandpassword)System.

10、out.print(Pleaseinputyournameandpassword,);System.out.println(Thisisthe+count+stclientconnetion!);System.out.print(Usingtheformat:namepassword:);System.in.read(buf);os.write(buf);/開始讀取文件數(shù)據(jù)并把它寫到一個名為clientread.txt的文件中FileOutputStreamfos=newFileOutputStream(clientread.txt);intdata;while(-1!=(data=is.re

11、ad()fos.write(data);System.out.println(nFilehasbeenrecervedsuccessfully.);os.close();is.close();s.close();catch(Exceptionex)ex.printStackTrace();*/客戶端程序:importjava ioFileOutputStream;importjava ioIOException;importjava ioInputStream;importjava ioOutputStream;.InetAddress;.InetSocketAddress;.Socket;p

12、ublicclassSendFileClient(privatestaticfinalintServer_PORT=6000;privatestaticfinalintClient_PORT=6001;/*使用方法:運行這個程序需要帶上參數(shù),參數(shù)類型為點分十進制的ip地址,例如:53*paramargs*throwslOException*/publicstaticvoidmain(Stringargs)throwslOException(/TODOAuto-generatedmethodstubSystem.out.println(Thisisclient);/*Sys

13、tem.out.print(Pleaseinputyournameandpassword,);System.out.print(Usingtheformat:namepassword:);bytebuf=newbyte100;System.in.read(buf);*/bytebuf=newbyte100;bytename=newbyte100;/InetAddressinetAddr;if(!isIPAddress(args0)(System.out.println(Theusageis:javaSendFileClientipaddress);System.out.println(Fore

14、xample:javaSendFileClient53);return;StringipStr=args0;try(/創(chuàng)建一個SocketSockets=newSocket();s.connect(newInetSocketAddress(ipStr,Server_PORT),Client_PORT);OutputStreamos=s.getOutputStream();/輸出流InputStreamis=s.getInputStream();/輸入流intlen=is.read(buf);/從輸入流中讀取數(shù)據(jù)到bufSystem.out.println(newStrin

15、g(buf,0,len);/向輸出流中寫入數(shù)據(jù),請求傳輸一個文件os.write(getserver.txt.getBytes();len=is.read(buf);/從輸入流中讀取數(shù)據(jù)到bufStringtempStr=newString(buf,0,len);if(tempStr.startsWith(Pleaseinputyournameandpassword)(System.out.println(Pleaseinputyournameandpassword,);System.out.println(Usingtheformat:namepassword:);do(System.in.read(name);while(namelength5);os.write(name);/開始讀取文件數(shù)據(jù)并把它寫到一個名為clientread.txt的文件中FileOutputStreamfos=newFileOutputStream(clientread.txt);intdata;while(-1!=(data=is.read()(fos.write(da

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論