赞
踩
客户端以XML格式向服务器端发送数据,详细要求如下:
1)客户端读取myemp.xml数据,将其发送到服务器端。
2)服务器接收到XML格式的数据后,进行解析,将解析到的数据输出到控制台。
- public class Emp {
- private int id;
- private String name;
- private int age;
- private String gender;
- private int salary;
- public Emp(int id, String name, int age, String gender, int salary) {
- super();
- this.id = id;
- this.name = name;
- this.age = age;
- this.gender = gender;
- this.salary = salary;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getGender() {
- return gender;
- }
- public void setGender(String gender) {
- this.gender = gender;
- }
- public int getSalary() {
- return salary;
- }
- public void setSalary(int salary) {
- this.salary = salary;
- }
- public String toString(){
- return id+","+name+","+age+","+gender+","+salary;
- }
- }
客户端:
- import java.io.File;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import org.dom4j.Document;
- import org.dom4j.io.SAXReader;
- import org.dom4j.io.XMLWriter;
- /**
- * 客户端
- */
- public class Client {
- private Socket socket;
- public Client() throws UnknownHostException, IOException{
- socket = new Socket("192.168.1.100",8088);
- }
- public void start(){
- try {
- //获取输出流
- OutputStream out = socket.getOutputStream();
- File file = new File("./src/day08/myemp.xml");
- //创建SAXReader,读取指定文件
- SAXReader reader = new SAXReader();
- Document doc = reader.read(file);
- //创建XML输出流
- XMLWriter writer = new XMLWriter();
- writer.setOutputStream(out);
- writer.write(doc);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if(socket != null){
- try {
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public static void main(String[] args) {
- try {
- Client c = new Client();
- c.start();
- } catch (Exception e) {
- e.printStackTrace();
- System.out.println("客户端初始化失败");
- }
- }
- }
服务端:
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.ArrayList;
- import java.util.List;
- import org.dom4j.Document;
- import org.dom4j.Element;
- import org.dom4j.io.SAXReader;
-
- /**
- * 服务端
- */
- public class Server {
- private ServerSocket server;
- public Server() throws IOException{
- server = new ServerSocket(8088);
- }
- public void start(){
- try {
- //监听客户端,建立连接
- System.out.println("等待连接..........");
- Socket socket = server.accept();
- System.out.println("服务端连接成功!");
- //获取输入流
- InputStream in = socket.getInputStream();
- //创建SAXReader,读取指定文件
- SAXReader reader = new SAXReader();
- Document doc = reader.read(in);
- //获取根节点
- Element root = doc.getRootElement();
- //获取根节点(list)下所有子节点emp
- List<Element> list = root.elements();
- //保存所有员工对象的集合
- List<Emp> emplist = new ArrayList<Emp>();
- for(Element e : list){
- int id = Integer.parseInt(e.attribute("id").getValue());
- String name = e.elementText("name");
- int age = Integer.parseInt(e.elementText("age"));
- String gender = e.elementText("gender");
- int salary = Integer.parseInt(e.elementText("salary"));
- emplist.add(new Emp(id,name,age,gender,salary));
- }
- System.out.println("解析完毕!");
- //输出到控制台
- for(Emp e : emplist){
- System.out.println(e);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if(server != null){
- try {
- server.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
-
- public static void main(String[] args) {
- try {
- Server s = new Server();
- s.start();
- } catch (IOException e) {
- e.printStackTrace();
- System.out.println("初始化服务端失败!");
- }
- }
- }
测试结果会在服务端输出:
- 等待连接..........
- 服务端连接成功!
- 解析完毕!
- 1,jack,33,男,5000
- 2,boss,55,男,5500
- 3,marry,22,女,3500
- 4,kate,25,女,6000
- 5,tom,31,男,8500
客户端以XML格式向服务器端发送数据,详细要求如下:
1)客户端读取myemp.xml中的数据,将其发送到服务器端。
2)服务器接收到XML格式的数据后,将其输出到serverMyemp.xml文件中。
代码如下:客户端:
- import java.io.File;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import org.dom4j.Document;
- import org.dom4j.io.SAXReader;
- import org.dom4j.io.XMLWriter;
-
- public class Client {
- private Socket socket;
- public Client() throws UnknownHostException, IOException{
- socket = new Socket("192.168.1.100",8088);
- }
- public void start(){
- try{
- OutputStream out = socket.getOutputStream();
- //创建SAXReader,读取指定文件
- SAXReader reader = new SAXReader();
- File file = new File("myemp.xml");
- Document doc = reader.read(file);
- //创建XML输出流
- XMLWriter writer = new XMLWriter();
- writer.setOutputStream(out);
- writer.write(doc);
- } catch(Exception e){
- e.printStackTrace();
- } finally {//只关socket
- if(socket != null){
- try {
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public static void main(String[] args) {
- Client c;
- try {
- c = new Client();
- c.start();
- } catch (Exception e) {
- e.printStackTrace();
- System.out.println("客户端初始化失败!");
- }
- }
- }
服务端
代码如下:
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- import org.dom4j.Document;
- import org.dom4j.io.SAXReader;
- import org.dom4j.io.XMLWriter;
-
- public class Server {
- private ServerSocket server;
- public Server() throws IOException{
- server = new ServerSocket(8088);
- }
- public void start(){
- XMLWriter writer = null;
- try{
- System.out.println("等待连接......");
- Socket socket = server.accept();
- System.out.println("连接客户端成功!");
- InputStream in = socket.getInputStream();
- //读取用户发送过来的xml
- SAXReader reader = new SAXReader();
- Document doc = reader.read(in);
-
- //写出xml到day08包中
- FileOutputStream fos = new FileOutputStream("./src/day09/serverMyemp.xml");
- writer = new XMLWriter();
- writer.setOutputStream(fos);
- writer.write(doc);
- System.out.println("服务端接受文件成功!");
- } catch(Exception e){
- e.printStackTrace();
- } finally {//writer和server都要关
- if(writer != null){
- try {
- writer.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if(server != null){
- try {
- server.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
-
- public static void main(String[] args) {
- try {
- Server s = new Server();
- s.start();
- } catch (IOException e) {
- e.printStackTrace();
- System.out.println("服务端初始化失败!");
- }
- }
- }
- 等待连接......
- 连接客户端成功!
- 服务端接受文件成功!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。