Java code to transfer an XML file to a remote server

Source: Internet
Author: User
Tags format net return string
xml| Server | remote Service

Package Com.yuchai.marketing.vehicleSCM.sendNC;

Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.BufferedReader;
Import Java.io.ByteArrayInputStream;
Import Java.io.CharArrayWriter;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;
Import Java.io.OutputStreamWriter;
Import Java.io.Reader;
Import java.net.InetAddress;
Import java.net.MalformedURLException;
Import java.net.ProtocolException;
Import Java.net.Socket;
Import Java.net.URL;
Import java.net.URLConnection;
Import java.net.UnknownHostException;
Import java.util.Properties;

Import Javax.xml.parsers.DocumentBuilder;
Import Javax.xml.parsers.DocumentBuilderFactory;
Import javax.xml.parsers.ParserConfigurationException;

Import org.dom4j.Document;
Import org.dom4j.DocumentException;
Import Org.dom4j.DocumentHelper;
Import Org.dom4j.io.SAXReader;
Import org.w3c.dom.*;

Import Com.yuchai.marketing.vehicleSCM.xml.builder.NCInvoiceXMLBuilder;
Import Com.yuchai.marketing.vehicleSCM.xml.builder.SuperBuilder;

/**
* 2006-04-04
* @author Weiwei

*/
public class Httpsend {

protected URL URL;

Private URLConnection Conn;

/**
* The Post method is to transfer data to the server so that the server can make the appropriate processing. For example, a common submission form on a Web page.
*/
public void POST (Document content) {
SendMessage (content);
}

 /**
  * Writes the XML file to the output stream, which enables the NC server to transfer data
  *
  * @param content
  */
  private void SendMessage (Document content) {
  try {
   outputstream raw = Conn.getoutputstream ();
   outputstream buf = new Bufferedoutputstream (raw);
   outputstreamwriter out = new OutputStreamWriter (BUF);
   /*
    * Saxreader saxreader = new Saxreader (); Document DOCUMENTFROMD =
    * Saxreader.read ("D://try.xml");
    * documentfromd.setxmlencoding ("gb2312");
    */
   //out.write (new String (content);
   out.write (Content.asxml ());
   out.flush ();
   out.close ();
   raw.close ();
  } catch (IOException e) {
   new IOException ("Transfer failed");
 &NBSP}
 }

/**
* Read the receipt file from the input stream
*
* @return
*/
Public Document Getreceivermessage () {
try {

InputStream raw = Conn.getinputstream ();
InputStream in = new Bufferedinputstream (raw);
Reader reader = new InputStreamReader (in);
BufferedReader bufreader = new BufferedReader (reader);

String xmlstring = "";

   //output in background
   int C;
   system.out.println ("==================beging====================");
   while ((c = bufreader.read ())!=-1) {
    system.out.print ((char) c);
    xmlstring + = (char) c;
   }
   in.close ();
   system.out.println ("===================end======================");
   //converts an XML string to an XML file
   document document = Documenthelper.parsetext ( xmlstring);
   return document;
  } catch (Documentexception e) {
   new documentexception ("Receipt file format is not valid");
 &NBSP} catch (IOException e) {
   new IOException ("Cannot get receipt file");
 &NBSP}
  return null;
 .}

/**
* Establish a connection and set the output ready to be true
*
* @param urlstring
*/
public void OpenServer (String urlstring) {
try {
Checkhttp (urlstring);
conn = Url.openconnection ();
Conn.setdooutput (TRUE);
catch (IOException e) {
New IOException ("Connection Server failed");
}
}

/**
* Store Files
*
* @param bytes
*/
public void SaveFile (Document doc) {
Element root = Doc.getrootelement ();

The generated filename can be named by itself, and here is the value in a tag

String filename = "d://generated XML file//" + "[REC]"
+ root.attributevalue ("filename");
try {
OutputFormat format = Outputformat.createprettyprint ();
Format.setencoding ("gb2312");
XMLWriter writer = new XMLWriter (
New FileWriter (filename), format);

Writer.write (DOC);
Writer.close ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}

/**
* Check URL legality
*
* @param urlstring
* @throws protocolexception
*/
protected void Checkhttp (String urlstring) throws Protocolexception {
try {
URL url = new URL (urlstring);
if (url = null | |!url.getprotocol (). toUpperCase (). Equals ("HTTP"))
throw new Protocolexception ("This is not an HTTP protocol");
This.url = URL;
catch (malformedurlexception m) {
throw new Protocolexception ("Protocol format error");
}
}
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.