Socket服務(wù)端和客戶端XML生成與解析java代碼_第1頁
Socket服務(wù)端和客戶端XML生成與解析java代碼_第2頁
Socket服務(wù)端和客戶端XML生成與解析java代碼_第3頁
Socket服務(wù)端和客戶端XML生成與解析java代碼_第4頁
Socket服務(wù)端和客戶端XML生成與解析java代碼_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、1.Socket的Util輔助類import .throws Exceptionexception;import java.io.*;/*/public void CreateConnection() throws Exception try socket = new Socket(ip, port); catch (Exception e) e.printStackTrace(); if (socket != null)socket.close();throw e; finally /發(fā)送消息public void sendMessage(String sendMessage) throws

2、 Exception try out = new DataOutputStream(socket.getOutputStream(); if (sendMessage.equals(Windows) out.writeByte(0 x1);* 2.socket 的 Util 輔助類* author willson*/public class ClientSocket private String ip;private int port;private Socket socket = null;DataOutputStream out = null;DataInputStream getMess

3、ageStream = null; public ClientSocket(String ip, int port) this.ip = ip;this.port = port;/*創(chuàng)建socket連接 out.flush(); return;if (sendMessage.equals(Unix) out.writeByte(0 x2);out.flush(); return;if (sendMessage.equals(Linux) out.writeByte(0 x3);out.flush(); else out.writeUTF(sendMessage); out.flush(); c

4、atch (Exception e) e.printStackTrace(); if (out != null) out.close();throw e; finally /接受消息public DataInputStream getMessageStream() throws Exception try getMessageStream = new DataInputStream(new BufferedInputStream( socket.getInputStream();return getMessageStream; catch (Exception e) e.printStackT

5、race(); if (getMessageStream != null)getMessageStream.close(); throw e; finally /關(guān)閉連接public void shutDownConnection() try if (out != null) out.close();if (getMessageStream != null)getMessageStream.close();if (socket != null) socket.close(); catch (Exception e) 2.Socket服務(wù)器端import java.io.BufferedInpu

6、tStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import .ServerSocket;import .Socket;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.transform.Transformer;import javax.x

7、ml.transform.TransformerFactory;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import org.w3c.dom.Comment;import org.w3c.dom.Document;import org.w3c.dom.Element;/ author willson* */public class ServerTest 設(shè)置端口號,一遍客戶端調(diào)用int port = 8821;void start() Socket socke

8、t = null;try /創(chuàng)建綁定到特定端口的服務(wù)器套接字。ServerSocket serverSocket = new ServerSocket(port); while (true) /選擇進行傳輸?shù)奈募tring filePath = writeXMLFile();File fi = new File(filePath);* 1.服務(wù)器端*System.out.println(File Name : + fi.getName() + ;tFile Size() : + (int) fi.length() + bytes);System.out.println(等待客戶端連接,連接端

9、口 : + port);偵聽并接受到此套接字的連接。此方法在連接傳入之前一直阻塞socket = serverSocket.accept();System.out.println(建立 socket 鏈接);/socket.getInputStream()返回此套接字的輸入流DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream(); dis.readByte(); DataInputStream fis = new DataInputStream(new BufferedI

10、nputStream(new FileInputStream(filePath);DataOutputStream ps = new DataOutputStream(socket.getOutputStream();/將文件名及長度傳給客戶端。這里要真正適用所有平臺,例如中文名 的處理,還需要加工,具體可以參見 Think In Java/ 4th里有現(xiàn)成的代碼。ps.writeUTF(fi.getName();ps.flush();ps.writeLong(long) fi.length();ps.flush();int bufferSize = 8192;byte buf = new b

11、ytebufferSize;while (true) int read = 0;if (fis != null) read = fis.read(buf);if (read = -1) break;ps.write(buf, 0, read);ps.flush();注意關(guān)閉socket鏈接哦,不然客戶端會等待server的數(shù)據(jù)過來,/直到socket超時,導(dǎo)致數(shù)據(jù)不完整。fis.close();socket.close();System.out.println(文件傳輸完成 n); catch (Exception e) e.printStackTrace();/DOM生成XML文件priva

12、te String writeXMLFile()/獲取dom解析的工廠DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder;/創(chuàng)建xml文件生成路徑String path=E:why.xml; try builder = factory.newDocumentBuilder();Document doc = builder.newDocument();作為XML聲明的一部分指定此文檔版本號的屬性doc.setXmlVersion(1.0);創(chuàng)建給定指定字符串的C

13、omment節(jié)點。/Comment此接口繼承自 CharacterData表示注釋的內(nèi)容,即起始 之間的所有字符。注意,這是在 XML (實際上是 HTML )中的注釋的定義, 盡管有些HTML工具可以實現(xiàn)完整SGML注釋結(jié)構(gòu)。Comment comment = doc.createComment(創(chuàng)建 xml 文件);doc.appendChild(comment);創(chuàng)建指定類型的元素Element root = doc.createElement(職員);Element name = doc.createElement(姓名力name.appendChild(doc.createTextN

14、ode(王海燕);Element sex = doc.createElement(性另1J);sex.setAttribute(name,女)root.appendChild(name);root.appendChild(sex);doc.appendChild(root);/TransformerFactory 實例可用于創(chuàng)建 Transformer 和 Templates 對象TransformerFactory factory1 = TransformerFactory.newInstance();/ factory1.newTransformer()創(chuàng)建執(zhí)彳亍從Source 至U Re

15、sult 的復(fù)制的新TransformerTransformer tformer = factory1.newTransformer();將 XML Source 轉(zhuǎn)換為 Result。當(dāng)實例化 Transformer和 對 Transformer實例進行任何修改時,指定的轉(zhuǎn)換行為由TransformerFactory的實際設(shè)置決定???Source 表示為由 DocumentBuilder.newDocument() 構(gòu)造的空文檔??誗ource的轉(zhuǎn)換結(jié)果取決于轉(zhuǎn)換行為;結(jié)果不總為空Resulttformer.transform(new DOMSource(doc), new StreamR

16、esult(path); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace(); return path;public static void main(String arg口) new ServerTest().start();3.Socket客戶端import java.io.BufferedOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import ja

17、va.io.FileOutputStream;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;/ author willson* */public class ClientTest private ClientSocket cs = null;privat

18、e String ip = localhost;/設(shè)置成服務(wù)器 IPprivate int port = 8821;private String sendMessage = Windwos;public ClientTest() try if (createConnection() sendMessage();getMessage(F:); catch (Exception ex) ex.printStackTrace();private boolean createConnection() * 3.客戶端*cs = new ClientSocket(ip, port);try cs.Crea

19、teConnection();System.out.print(連接服務(wù)器成功! + n);return true; catch (Exception e) System.out.print(連接服務(wù)器失敗! + n);return false;private void sendMessage() if (cs = null)return;try cs.sendMessage(sendMessage); catch (Exception e) System.out.print(發(fā)送消息失敗! + n);private void getMessage(String savePath) if (c

20、s = null)return;DataInputStream inputStream = null;try inputStream = cs.getMessageStream(); catch (Exception e) System.out.print(接收消息緩存錯誤n);return;try /本地保存路徑,文件名會自動從服務(wù)器端繼承而來。int bufferSize = 8192;byte口 buf = new bytebufferSize;int passedlen = 0;long len = 0;savePath += inputStream.readUTF();DataOut

21、putStream fileOut = new DataOutputStream(newBufferedOutputStream(new BufferedOutputStream(new FileOutputStream(savePath);len = inputStream.readLong();System.out.println(File Size() : + len + bytes);System.out.println(開始接收文件! + n);while (true) int read = 0;if (inputstream != null) read = inputStream.

22、read(buf);passedlen += read;if (read = -1) break;/下面進度條本為圖形界面的prograssBar做的,這里如果是打文件,可能會重復(fù)打印出一些相同的百分比System.out.println(文件接收了 + (passedlen * 100 / len) + %n);fileOut.write(buf, 0, read);System.out.println(接收完成,文件存為 + savePath + n);fileOut.close(); catch (Exception e) System.out.println(接收消息錯誤+ n);re

23、turn;jiexi(savePath);public static void main(String arg口)new ClientTest();private static void jiexi(String savePath)獲取dom解析的工廠DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();是否刪除空格false truefactory.setIgnoringElementContentWhitespace(true);獲取解析器DocumentBuilder builder;try builder = factory.newDocumentBuilder();/把xml文檔輸入到解析器中,并獲取xml文檔對應(yīng)的document對象Document document = builder.pa

溫馨提示

  • 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論