//jdom生成xml的方法如下:
package socketXmlDemo.socketXmlDemo.createXml;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ResultXml {
// slf4j的log
private final static Logger logger = LoggerFactory.getLogger(ResultXml.class);
/**
* jdom生成xml
* @throws FileNotFoundException
* @throws IOException
*/
public static void raiseXml() throws FileNotFoundException, IOException {
// 生成xml文件
Element root = new Element("doc");
root.setAttribute("xmlcolonlang", "ja");
root.setAttribute("xmlnscolonfo", "http://www.w3.org/1999/XSL/Format");
Document Doc = new Document(root);
Element elements = new Element("reckon_pe").setText("helloworld");
// 報告截止日期(頁面最下方日期)
root.addContent(elements);
XMLOutputter XMLOut = new XMLOutputter();
XMLOut.setFormat(Format.getCompactFormat().setEncoding("UTF-8"));
String xmlPath = "c:\\testSocket.xml";
// 生成的xml保存的路勁,可用配置文件管理
FileOutputStream fos = null;
try{
fos =new FileOutputStream(new File(xmlPath));
logger.error("正在創建文件路徑!");
XMLOut.output(Doc, new FileOutputStream(xmlPath));
}catch(Exception e){
}finally{
fos.close();
}
}
public static void main(String[] args) throws Exception {
raiseXml();
}
}
//socket監聽消息的方法如下:
package socketXmlDemo.socketXmlDemo.sendXml;
import java.net.*;
import java.io.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ServerTcpListener implements Runnable {
// slf4j的log
private final static Logger logger = LoggerFactory.getLogger(ServerTcpListener.class);
public static void main(String[] args) {
try {
final ServerSocket server = new ServerSocket(ClientTcpSend.port);
Thread th = new Thread(new Runnable() {
public void run() {
while (true) {
try {
logger.info("開始監聽...");
Socket socket = server.accept();
logger.info("有鏈接");
receiveFile(socket);
} catch (Exception e) {
}
}
}
});
th.run(); //啓動線程運行
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
}
public static void receiveFile(Socket socket) {
byte[] inputByte = null;
int length = 0;
DataInputStream dis = null;
FileOutputStream fos = null;
try {
try {
dis = new DataInputStream(socket.getInputStream());
fos = new FileOutputStream(new File("e:\\testSocket.xml"));
inputByte = new byte[1024*4];
logger.info("開始接收數據...");
while ((length = dis.read(inputByte, 0, inputByte.length)) > 0) {
fos.write(inputByte, 0, length);
fos.flush();
}
logger.info("完成接收");
} finally {
if (fos != null)
fos.close();
if (dis != null)
dis.close();
if (socket != null)
socket.close();
}
} catch (Exception e) {
}
}
}
server端方法如下:
package socketXmlDemo.socketXmlDemo.sendXml;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
public class ClientTcpSend {
public static String clientip = "127.0.0.1";
public static int port = 33456;
public static void main(String[] args) {
int length = 0;
byte[] sendBytes = null;
Socket socket = null;
DataOutputStream dos = null;
FileInputStream fis = null;
try {
try {
socket = new Socket();
socket.connect(new InetSocketAddress(clientip, port),30 * 1000);
dos = new DataOutputStream(socket.getOutputStream());
File file = new File("c:\\testSocket.xml");
fis = new FileInputStream(file);
sendBytes = new byte[1024*4];
while ((length = fis.read(sendBytes, 0, sendBytes.length)) > 0) {
dos.write(sendBytes, 0, length);
dos.flush();
}
} finally {
if (dos != null)
dos.close();
if (fis != null)
fis.close();
if (socket != null)
socket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
pom.xml配置如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>socketXmlDemo</groupId>
<artifactId>socketXmlDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>socketXmlDemo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- 生成xml使用 -->
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.1.3</version>
</dependency>
<!-- slf4j -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>0.9.29</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>0.9.29</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>1.6.1</version>
</dependency>
</dependencies>
</project>