Byte conversion and request tools

Source: Internet
Author: User

Tool class:

Import java. io. BufferedReader;

Import java. io. ByteArrayOutputStream;

Import java. io. File;

Import java. io. FileInputStream;

Import java. io. FileOutputStream;

Import java. io. IOException;

Import java. io. InputStream;

Import java. io. InputStreamReader;

Import java. io. OutputStream;

Import java.net. HttpURLConnection;

Import java.net. URL;

Import java.net. URLConnection;

 

Public class TestTool {

/**

* Read the specified file and convert it to a byte array.

*

* Aug 11,201 1

* @ Param file path

* @ Param file name

* @ Return the byte array converted from the file content

* @ Throws Exception

*/

Public static byte [] read (String path, String fileName) throws Exception {

Byte [] aaa = getContent (path, fileName );

Return aaa;

 

}

 

/**

* Send a request

*

* Aug 11,201 1

* @ Param: byte array sent

* @ Throws connection exception

*/

Public static void sendHttpRequest (byte [] strRequest) throws Exception {

 

URL url = null;

HttpURLConnection httpURLConnection = null;

OutputStream out = null;

// The URL to be requested

String strUrl = "http: // 10.10.92.105: 8080/xxx/xxServlet ";

Try {

Url = new URL (strUrl );

 

URLConnection urlcn = url. openConnection ();

 

If (urlcn instanceof HttpURLConnection ){

 

HttpURLConnection = (HttpURLConnection) urlcn;

 

HttpURLConnection. setRequestMethod ("POST ");

 

HttpURLConnection. setDoOutput (true );

HttpURLConnection. setDoInput (true );

 

HttpURLConnection. setConnectTimeout (900000 );

HttpURLConnection. setread timeout (900000 );

 

HttpURLConnection. setRequestProperty ("Content-type ",

"Application/x-java-serialized-object ");

 

Out = httpURLConnection. getOutputStream ();

 

Out. write (strRequest );

 

// The returned status is normal.

If (httpURLConnection. getResponseCode () = HttpURLConnection. HTTP_ OK ){

System. out. println ("----------------------------------------------------");

BufferedReader reader = new BufferedReader (

New InputStreamReader (httpURLConnection

. GetInputStream ()));

String line;

StringBuffer str = new StringBuffer ();

While (line = reader. readLine ())! = Null ){

Str. append (line );

}

// Print the response content

System. out. println (str. toString ());

}

}

 

} Catch (Exception e ){

Throw new Exception ("internal server error ");

} Finally {

// Close the connection

If (httpURLConnection! = Null ){

HttpURLConnection. disconnect ();

}

// Close the stream

If (out! = Null ){

Try {

Out. flush ();

Out. close ();

} Catch (IOException e ){

Throw new Exception ("internal server error ");

}

}

}

}

 

/**

* Write a byte array to the file.

*

* Aug 11,201 1

* @ Param: array to be written

* @ Param: Path of the file to be written

* @ Param: file name written to the file

*/

Public static void write (byte [] bt, String filePath, String fileName ){

// File file = new File ("D:", "mm ");

File file = new File (filePath, fileName );

 

OutputStream oo = null;

Try {

Oo = new FileOutputStream (file, true );

Oo. write (bt );

} Catch (Exception e ){

E. printStackTrace ();

} Finally {

If (oo! = Null ){

Try {

Oo. flush ();

Oo. close ();

} Catch (Exception e ){

E. printStackTrace ();

}

}

}

}

 

/**

*

* Get 8 bits of the number of symbols

* Aug 11,201 1

* @ Param B

* @ Return

*/

Public static String getBit (byte B ){

StringBuffer sbf = new StringBuffer ();

Int [] bit = new int [8];

For (int I = 0; I <bit. length; I ++ ){

Bit [8-I-1] = (B> I) & 1;

}

For (int I: bit ){

Sbf. append (I );

}

Return sbf. toString ();

 

}

 

/**

* Convert a signed number to an unsigned number.

*

* Aug 11,201 1

* @ Author Lu Jianming

* @ Param p the bytes to be converted

* @ Return

*/

Public static int getU1 (byte p ){

Return p & 0xff;

}

 

/**

*

* <P> Discription: [convert the unsigned number to the signed number] </p>

* @ Param intValue

* @ Return

* @ Author: [Chinese name of the creator]

* @ Update: [date YYYY-MM-DD] [modifier name] [Change description]

*/

Public static int getIntA (int intValue ){

Int byteValue;

Int temp = intValue % 256;

If (intValue <0 ){

ByteValue = temp <-128? 256 + temp: temp;

} Else {

ByteValue = temp> 127? Temp-256: temp;

}

Return byteValue;

}

 

/**

*

* <P> Discription: [obtains the object's byte stream] </p>

* @ Param filePath: file path

* @ Param fileName file name

* @ Return refers to the byte array to which the file is converted.

* @ Author: [Chinese name of the creator]

* @ Update: [date YYYY-MM-DD] [modifier name] [Change description]

*/

Private static byte [] getContent (String filePath, String fileName ){

File file = new File (filePath, fileName );

InputStream strm = null;

ByteArrayOutputStream baos = new ByteArrayOutputStream ();

Try {

Byte [] ll = new byte [1024];

Strm = new FileInputStream (file );

Int len =-1;

While (len = strm. read (ll ))! =-1 ){

Baos. write (ll, 0, len );

}

} Catch (Exception e1 ){

E1.printStackTrace ();

}

Return baos. toByteArray ();

}

 

}

 

 

Test the main function:

 

Public class TestMain {

 

/**

*

* Aug 11,201 1

* @ Param args

* @ Throws Exception

*/

Public static void main (String [] args) throws Exception {

Byte a =-1;

// Obtain 8 bits with the symbol bit

System. out. println (TestTool. getBit ());

 

// Convert the signed number to the unsigned number (positive integer or itself)

System. out. println (TestTool. getU1 ());

// Convert the unsigned number to the signed number (0-255)

System. out. println (TestTool. getIntA (255 ));

Byte [] bt = new byte [] {-1 };

// Write the byte array to the file

TestTool. write (bt, "D:", "11 ");

// Read the specified file into a byte array

Byte [] bread = TestTool. read ("D:", "mm ");

For (int I = 0; I <bread. length; I ++ ){

System. out. println (bread [I] + "-" + TestTool. getBit (bread [I]);

}

// Send the request

TestTool. sendHttpRequest (bread );

}

 

}

 

This article is from the "situ Jianming" blog

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.