工具类:
- 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 {
- /**
- * 读取指定文件转成字节数组
- *
- * Aug 11, 2011
- * @param 文件的路径
- * @param 文件的名称
- * @return 文件内容转成的字节数组
- * @throws Exception
- */
- public static byte[] read(String path, String fileName) throws Exception {
- byte[] aaa = getContent(path, fileName);
- return aaa;
- }
- /**
- * 发送请求
- *
- * Aug 11, 2011
- * @param 发送的字节数组
- * @throws 连接异常
- */
- public static void sendHttpRequest(byte[] strRequest) throws Exception {
- URL url = null;
- HttpURLConnection httpURLConnection = null;
- OutputStream out = null;
- // 要请求的URL
- 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.setReadTimeout(900000);
- httpURLConnection.setRequestProperty("Content-type",
- "application/x-java-serialized-object");
- out = httpURLConnection.getOutputStream();
- out.write(strRequest);
- // 返回状态正常
- 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);
- }
- // 打印应答内容
- System.out.println(str.toString());
- }
- }
- } catch (Exception e) {
- throw new Exception("内部服务器错误");
- } finally {
- // 关闭连接
- if (httpURLConnection != null) {
- httpURLConnection.disconnect();
- }
- // 关闭流
- if (out != null) {
- try {
- out.flush();
- out.close();
- } catch (IOException e) {
- throw new Exception("内部服务器错误");
- }
- }
- }
- }
- /**
- * 往文件里写byte数组
- *
- * Aug 11, 2011
- * @param 需要写入的数组
- * @param 写入文件的路径
- * @param 写入文件的文件名
- */
- 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();
- }
- }
- }
- }
- /**
- *
- * 获取有符号数的8个bit位
- * Aug 11, 2011
- * @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();
- }
- /**
- * 将有符号数转换为无符号数
- *
- * Aug 11, 2011
- * @author 吕建明
- * @param p 需要转换的字节
- * @return
- */
- public static int getU1(byte p) {
- return p & 0xff;
- }
- /**
- *
- * <p>Discription:[将无符号数转换为有符号数]</p>
- * @param intValue
- * @return
- * @author:[创建者中文名字]
- * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
- */
- 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:[获取文件的字节流]</p>
- * @param filePath 文件的路径
- * @param fileName 文件名称
- * @return 文件被容转换成的字节数组
- * @author:[创建者中文名字]
- * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
- */
- 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();
- }
- }
测试主函数:
- public class TestMain {
- /**
- *
- * Aug 11, 2011
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- byte a = -1;
- // 获取有符号位的8个bit位
- System.out.println(TestTool.getBit(a));
- // 将有符号数转换为无符号数(正整数还是自己本身)
- System.out.println(TestTool.getU1(a));
- // 将无符号数转换为有符号数(0-255)
- System.out.println(TestTool.getIntA(255));
- byte[] bt = new byte[]{-1};
- //往文件里写byte数组
- TestTool.write(bt, "D:", "11");
- // 读取指定文件转成字节数组
- byte[] bread = TestTool.read("D:", "mm");
- for(int i = 0; i< bread.length; i++){
- System.out.println(bread[i]+"-"+TestTool.getBit(bread[i]));
- }
- // 发送请求
- TestTool.sendHttpRequest(bread);
- }
- }
本文出自 “司徒建明” 博客,请务必保留此出处http://yujie020.blog.51cto.com/2827685/636835