import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class TestApp {
public static void main(String[] args) throws UnknownHostException,
IOException, ParserConfigurationException, SAXException, TransformerFactoryConfigurationError, TransformerException {
try {
byte a[] = { 127, 0, 0, 1 };
InetAddress address = InetAddress.getByAddress(a);
Socket socket = new Socket(address, 7777);
OutputStream os = socket.getOutputStream();
PrintWriter out = new PrintWriter(os, true);
File file = new File("c:\\test.xml");
System.out.println(file.getName());
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(file);
Transformer transfomer = TransformerFactory.newInstance()
.newTransformer();
transfomer.setOutputProperty("encoding", "GB2312");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
transfomer.transform((Source) (new DOMSource(doc)),
new StreamResult(stream));
out.println(stream.toByteArray());
out.close();
socket.close();
} catch (UnknownHostException e) {
System.out.println(e);
} catch (IOException e) {
System.out.println(e);
}
}
}
记录一下