J2EE點(diǎn)到點(diǎn)文件傳輸程序_第1頁
J2EE點(diǎn)到點(diǎn)文件傳輸程序_第2頁
J2EE點(diǎn)到點(diǎn)文件傳輸程序_第3頁
J2EE點(diǎn)到點(diǎn)文件傳輸程序_第4頁
J2EE點(diǎn)到點(diǎn)文件傳輸程序_第5頁
已閱讀5頁,還剩82頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、 J2EE課程設(shè)計(jì)報(bào)告 設(shè)計(jì)內(nèi)容:點(diǎn)到點(diǎn)文件傳輸 姓名:陳佳宇 學(xué)號(hào):20112730 班級(jí):軟件二班 西南交通大學(xué)2013-2014第二學(xué)期 設(shè)計(jì)報(bào)告一、課程設(shè)計(jì)目的 1.掌握基于TCP的網(wǎng)絡(luò)程序設(shè)計(jì)。 2.掌握基于UDP的網(wǎng)絡(luò)程序設(shè)計(jì)。 3.編寫遠(yuǎn)程點(diǎn)到點(diǎn)文件傳輸程序。2、 運(yùn)行環(huán)境 1.系統(tǒng)環(huán)境:win7,windows XP 2.編程環(huán)境:java JDK,Eclipse,cmd控制臺(tái)命令3、 設(shè)計(jì)內(nèi)容及步驟 1.編寫基于TCP協(xié)議的網(wǎng)絡(luò)程序,實(shí)現(xiàn)兩臺(tái)計(jì)算機(jī)之間互相發(fā)送文件。Socket通信的一般過程:創(chuàng)建Socket對(duì)象關(guān)閉Socket創(chuàng)建輸入輸出創(chuàng)建Socket對(duì)象是否創(chuàng)建Serv

2、ersocket等待客戶端連接請(qǐng)求 失敗拋出異常成功關(guān)閉Socket創(chuàng)建輸入輸出 客戶端創(chuàng)建Server端ServerSocket過程代碼:public class Server extends Thread public Server() try jbInit(); catch (Exception ex) ex.printStackTrace(); public int port=5000; public int maxThreadNum=5; private ServerSocket checkServer = null; private SocketThread socketThrea

3、d = null; private Socket aSocket = null; private List socketPool; /socket連接池 private boolean serverRunning = false; /服務(wù)器是否運(yùn)行 /構(gòu)造函數(shù) public Server(List socketPool) this.socketPool = socketPool; public void run() serverRunning = true; try checkServer = new ServerSocket(port); checkServer.setSoTimeout(6

4、0000); catch (IOException ex) ex.printStackTrace(); while (serverRunning) try System.out.println("Started."); aSocket = checkServer.accept(); System.out.println("client connected"); socketThread = new SocketThread(aSocket); socketThread.start(); if(socketPool.size()>=maxThread

5、Num) socketThread.stopThread(); else socketPool.add(socketThread); catch (Exception ex) ex.printStackTrace(); try Thread.sleep(1000); catch (InterruptedException ex) ex.printStackTrace(); try checkServer.close(); catch (IOException ex1) public void stopThread() serverRunning=false; private void jbIn

6、it() throws Exception 2. 編寫程序界面代碼1) 編寫主界面MainFrame 主界面由一個(gè)JPanel控件和一個(gè)JTabbedPane控件組成。 JPanel控件放在上面,用于存放操作控件,發(fā)送文件按鈕,設(shè)置按鈕,以及本機(jī)IP顯示控件。 JTabbedPane放在下面,用于存放接受控件,存放在里面的控件都是不可見的。當(dāng)發(fā)送主機(jī)向目的主機(jī)發(fā)送文件時(shí),里面控件可見,可以看到發(fā)送過來的消息,以及操作按鈕。2) 編寫發(fā)送文件子界面TransFileDialog3) 編寫設(shè)置子界面SettingDialog4) 編寫接受信息界面TransFileManager和TransFile

7、PanelTransFileManager核心代碼:public void run() running = true; SocketThread st; TransFilePanel tfp; int threadListSize; int paneListSize; while (running) threadListSize = threadList.size(); paneListSize = paneList.size(); for (int i = 0; i < threadListSize; i+) st = (SocketThread) threadList.get(i);

8、 if (paneListSize <= i) try Thread.sleep(100); catch (InterruptedException ex2) tfp = new TransFilePanel(st.isFileSender(); jtp.addTab(st.getFileName(), tfp); paneList.add(tfp); tfp.jtaFileTransStatus.setText("文件發(fā)送 目標(biāo)IP:"+st.getIP()+" 端口:"+st.getPort()+"nn正在與對(duì)方建立連接."

9、); else tfp = (TransFilePanel) paneList.get(i); tfp.setStatus(st.getStatus(); if (tfp.isCanneled) st.cancelTrans(); switch (st.getStatus() case SocketThread.FILE_TRANS_STATUS_WAITFORCONFIRM: if (st.isFileSender() tfp.jtaFileTransStatus.setText(st.getFileTransMessage()+"nn連接成功! 等待對(duì)方回應(yīng)."); e

10、lse tfp.jtaFileTransStatus.setText(st.getFileName() + " (" + st.fileSize + " B) 是否接收?n"+"來自:"+st.getIP()+"n簡(jiǎn)短附言:"+st.getMessage(); if (tfp.isConfirm) if(tfp.fileName!=null) File aFile = new File(tfp.fileName); long off=0,size; if (aFile.exists() File temFile=n

11、ew File(aFile.getAbsolutePath()+".tmp"); if(temFile!=null) try DataInputStream dis=new DataInputStream(new FileInputStream(temFile); off = dis.readLong(); System.out.println(off); size=dis.readLong(); if(off!=new File(tfp.fileName).length()|size!=st.fileSize) off=0; catch (IOException ex1)

12、 off=0; st.confirmReceiveFile(true,tfp.fileName,off); else st.confirmReceiveFile(false,"",0); break; case SocketThread.FILE_TRANS_STATUS_CONTEXT: double progress = st.getTransFileLength() * 1.0 / st.fileSize * 100; double transSpeed = (st.getTransFileLength() - tfp.transFileLength) / 1024

13、* 2; tfp.transFileLength = st.getTransFileLength(); tfp.jpgbFileTrans.setValue(int) progress); tfp.jtaFileTransStatus.setText(st.getFileTransMessage()+"nn"); tfp.jtaFileTransStatus.setText(tfp.jtaFileTransStatus.getText()+"傳輸速度:" + (int) transSpeed + "k/s"+" 已完成:&q

14、uot; + (int) progress + "%"); break; case SocketThread.FILE_TRANS_STATUS_SUCCESS: tfp.jtaFileTransStatus.setText(st.getFileTransMessage()+"nn傳輸成功!"); if(!st.isFileSender() new File(tfp.fileName+".tmp").delete(); if (tfp.isClosed) threadList.remove(i); paneList.remove(i)

15、; jtp.remove(i); i-; threadListSize-; paneListSize-; break; case SocketThread.FILE_TRANS_STATUS_FAIL: tfp.jtaFileTransStatus.setText(st.getFileTransMessage()+"nn傳輸失敗!" + st.getErrorMessage(); if (tfp.isClosed) threadList.remove(i); paneList.remove(i); jtp.remove(i); i-; threadListSize-; pa

16、neListSize-; break; try Thread.sleep(500); catch (InterruptedException ex) 3. 編寫服務(wù)器之間連接SocketThread代碼主要功能實(shí)現(xiàn)代碼如下:/接收者構(gòu)造函數(shù) public SocketThread(Socket aSocket) this.aSocket = aSocket; try aSocket.setSoTimeout(300000); catch (SocketException ex) byte address = aSocket.getInetAddress().getAddress(); IP =

17、 (address0 & 0xff) + "." + (address1 & 0xff) + "." + (address2 & 0xff) + "." + (address3 & 0xff); try dis = new DataInputStream(aSocket.getInputStream(); dos = new DataOutputStream(aSocket.getOutputStream(); fileTransStatus = FILE_TRANS_STATUS_FILENAME;

18、catch (IOException ex) setError("創(chuàng)建連接錯(cuò)誤!"); try aSocket.setReceiveBufferSize(CACHE_SIZE*2); catch (SocketException ex1) ex1.printStackTrace(); dataBuf=new byteCACHE_SIZE+100;/發(fā)送者構(gòu)造函數(shù) public SocketThread(String serverName, int portNo, String fileName, String message) aFile = new File(fileNa

19、me); this.fileName = aFile.getName(); this.fileSize = fileSize; fileSender = true; if (message != null) this.message = message; this.IP = serverName; this.port = portNo; dataBuf=new byteCACHE_SIZE; /線程執(zhí)行函數(shù) public void run() running = true; if (fileSender) try aSocket = new Socket(IP, port); aSocket.

20、setSoTimeout(300000); aSocket.setSendBufferSize(CACHE_SIZE*2); dos = new DataOutputStream(aSocket.getOutputStream(); dis = new DataInputStream(aSocket.getInputStream(); fDis = new DataInputStream(new FileInputStream(aFile); fileTransStatus = FILE_TRANS_STATUS_FILENAME; catch (UnknownHostException ex

21、1) ex1.printStackTrace(); setError("連接服務(wù)器錯(cuò)誤!"); catch (IOException ex1) ex1.printStackTrace(); setError("創(chuàng)建連接錯(cuò)誤!"); while (running) if (fileSender) sendFile(); else receiveFile(); try Thread.sleep(6); catch (InterruptedException ex) 4.打包程序,包名為trans4 課程設(shè)計(jì)總結(jié) 通過本次課程設(shè)計(jì),使我第一次了解和初步掌握了J

22、AVA網(wǎng)絡(luò)編程的基礎(chǔ)知識(shí),如TCP和UDP通信的過程,會(huì)編寫簡(jiǎn)單的網(wǎng)絡(luò)應(yīng)用程序,如聊天程序和遠(yuǎn)程文件傳輸程序等。 測(cè)試報(bào)告1、 界面2、 操作測(cè)試發(fā)送文件:填寫目的IP,可以是自己電腦的IP,可以是同局域網(wǎng)其他計(jì)算機(jī)的IP。為測(cè)試方便,這里填寫自己IP查看下傳輸結(jié)果:開始接受3 總結(jié) 本點(diǎn)到點(diǎn)傳輸系統(tǒng)能夠很好的實(shí)現(xiàn)相同計(jì)算機(jī),不同計(jì)算機(jī)之間進(jìn)行文件的傳輸。也可以發(fā)送對(duì)話,設(shè)置計(jì)算機(jī)端口信息等。 附錄(附源代碼)本程序?yàn)橐粋€(gè)包程序,包文件夾為trans,運(yùn)行方法:分別把每個(gè)JAVA程序放在一個(gè)txt記事本里面,創(chuàng)建一個(gè)名為trans文件夾,把他們?nèi)糠旁诶锩鎐md環(huán)境下運(yùn)行方法:我的trans文

23、件放在D盤Applicaiton1.javapackage trans;import java.awt.Toolkit;import javax.swing.SwingUtilities;import javax.swing.UIManager;import java.awt.Dimension;public class Application1 boolean packFrame = false; /* * Construct and show the application. */ public Application1() MainFrame frame = new MainFrame(

24、); / Validate frames that have preset sizes / Pack frames that have useful preferred size info, e.g. from their layout if (packFrame) frame.pack(); else frame.validate(); / Center the window Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = frame.getSize(); if

25、 (frameSize.height > screenSize.height) frameSize.height = screenSize.height; if (frameSize.width > screenSize.width) frameSize.width = screenSize.width; frame.setLocation(screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); frame.setVisible(true); /* * Applica

26、tion entry point. * * param args String */ public static void main(String args) SwingUtilities.invokeLater(new Runnable() public void run() try UIManager.setLookAndFeel(UIManager. getSystemLookAndFeelClassName(); catch (Exception exception) exception.printStackTrace(); new Application1(); ); MainFra

27、me.javapackage trans;import java.awt.BorderLayout;import java.awt.Dimension;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JToolBar;import java.awt.*;import javax.swing.JButton;import javax.swing.JLabel;import javax.swing.*;import java.util.*;import java.awt.event.ActionEvent

28、;import java.awt.event.ActionListener;import java.io.File;import .InetAddress;import java.awt.event.WindowEvent;import java.awt.event.WindowAdapter;import java.io.*;public class MainFrame extends JFrame JPanel contentPane; JButton jbtnSend = new JButton(); JLabel jlblIP = new JLabel(); JButton jbtnS

29、etting = new JButton(); JTabbedPane jtpTransFile = new JTabbedPane(); TransFileManager tfm=new TransFileManager(jtpTransFile); public MainFrame() try setDefaultCloseOperation(EXIT_ON_CLOSE); jbInit(); catch (Exception exception) exception.printStackTrace(); private void jbInit() throws Exception con

30、tentPane = (JPanel) getContentPane(); contentPane.setLayout(null); this.getContentPane().setBackground(new Color(206, 227, 249); setSize(new Dimension(400, 300); setTitle("P2P文件傳輸軟件"); this.addWindowListener(new MainFrame_this_windowAdapter(this); jbtnSend.setBackground(new Color(236, 247,

31、 255); jbtnSend.setBounds(new Rectangle(14, 14, 85, 25); jbtnSend.setFont(new java.awt.Font("宋體", Font.PLAIN, 13); jbtnSend.setBorder(BorderFactory.createRaisedBevelBorder(); jbtnSend.setText("發(fā)送文件"); jbtnSend.addActionListener(new MainFrame_jbtnSend_actionAdapter(this); jlblIP.s

32、etText("本機(jī)IP:"); jbtnSetting.setBackground(new Color(236, 247, 255); jbtnSetting.setBounds(new Rectangle(106, 14, 73, 25); jbtnSetting.setFont(new java.awt.Font("宋體", Font.PLAIN, 13); jbtnSetting.setBorder(BorderFactory.createRaisedBevelBorder(); jbtnSetting.setText("設(shè)置"

33、;); jbtnSetting.addActionListener(new MainFrame_jbtnSetting_actionAdapter(this); jtpTransFile.setBackground(new Color(206, 227, 249); jtpTransFile.setBounds(new Rectangle( 0, 54, 402, 246); jlblIP.setFont(new java.awt.Font("宋體", Font.PLAIN, 13); jlblIP.setBounds(new Rectangle(197, 18, 180,

34、 16); contentPane.setBackground(new Color(206, 227, 249); contentPane.setToolTipText(""); contentPane.add(jtpTransFile); contentPane.add(jbtnSend); contentPane.add(jbtnSetting); contentPane.add(jlblIP); tfm.start(); byte ip=InetAddress.getLocalHost().getAddress(); jlblIP.setText("本機(jī)IP

35、:"+(ip0&0xff)+"."+(ip1&0xff)+"."+(ip2&0xff)+"."+(ip3&0xff); public void jbtnSetting_actionPerformed(ActionEvent e) SettingDialog sd=new SettingDialog(this); sd.jtfPort.setText(String.valueOf(tfm.port); sd.jspnThreadNum.setValue(new Integer(tfm.maxTh

36、readNum); sd.show(); if(sd.flag) if(tfm.port!=sd.port) tfm.setPort(sd.port); tfm.setMaxThreadNum(sd.threadNum); public void jbtnSend_actionPerformed(ActionEvent e) TransFileDialog tfd=new TransFileDialog(this); tfd.show(); if(tfd.flag) for(int i=0;i<tfd.files.length;i+) tfm.sendFile(tfd.serverNam

37、e,tfd.port,tfd.filesi.getAbsolutePath(),tfd.message); public void this_windowClosed(WindowEvent e) tfm.close(); class MainFrame_this_windowAdapter extends WindowAdapter private MainFrame adaptee; MainFrame_this_windowAdapter(MainFrame adaptee) this.adaptee = adaptee; public void windowClosed(WindowE

38、vent e) adaptee.this_windowClosed(e); class MainFrame_jbtnSend_actionAdapter implements ActionListener private MainFrame adaptee; MainFrame_jbtnSend_actionAdapter(MainFrame adaptee) this.adaptee = adaptee; public void actionPerformed(ActionEvent e) adaptee.jbtnSend_actionPerformed(e); class MainFram

39、e_jbtnSetting_actionAdapter implements ActionListener private MainFrame adaptee; MainFrame_jbtnSetting_actionAdapter(MainFrame adaptee) this.adaptee = adaptee; public void actionPerformed(ActionEvent e) adaptee.jbtnSetting_actionPerformed(e); Server.java/服務(wù)器端package trans;import java.io.*;import .*;import java.util.List;public class Server extends Thread public Server() try jbInit(); catch (Exception ex) ex.printStackTrace(); public int port=5000; public int maxThreadNum=5

溫馨提示

  • 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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論