- 论坛徽章:
- 0
|
//CommClient.java
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
//import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class CommClient extends Thread
{
private InetAddress ip=null;
private Socket s=null;
// private InputStream is=null;
private OutputStream os=null;
public CommClient()
{
try
{
//ip=InetAddress.getByName("10.100.101.36");
ip=InetAddress.getLocalHost();
s=new Socket(ip,2227);
// is=s.getInputStream();
}
catch(Exception ex)
{
ex.printStackTrace();
System.err.println("public Client.");
}
}
//将File对象转换为字节数组
public static byte[] getBytesFromFile(File f)
{
if (f == null) {
return null;
}
try{
FileInputStream stream = new FileInputStream(f);
ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = stream.read(b)) != -1)
out.write(b, 0, n);
stream.close();
out.close();
return out.toByteArray();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
/**
* @param orgInfo :待封装的原始信息(包括:文件名和图像数据)
* @param flag :数据包封装格式类型
* 1 文本格式封装 2 图像格式封装 3 视频格式封装
* @return
*/
public byte[] generateDataPacket(byte[] orgInfo,String bsID,
short servType,int pacLength,byte Infoflag)
{
byte [] dataPacket=new byte[orgInfo.length +37];
switch(Infoflag)
{
case 1:
dataPacket=new byte[orgInfo.length+88];
break;
case 2:
byte [] bbsID=new byte[30];
System.arraycopy(bsID.getBytes(), 0, bbsID, 0, bsID.getBytes().length);
byte [] bservType=ShortToByte(servType);//字节数组长度为2
System.out.println("bservType length:"+bservType.length);
byte [] bpacLength=IntToByte(pacLength);//字节数组长度为4
System.arraycopy(bbsID, 0, dataPacket, 0, bbsID.length);
System.arraycopy(bservType, 0, dataPacket, 30, bservType.length);
System.arraycopy(bpacLength, 0, dataPacket, 32, bpacLength.length);
dataPacket[36]=Infoflag;
System.arraycopy(orgInfo, 0, dataPacket, 37, orgInfo.length);
//dataPacket=generateImagePacket(orgInfo);
break;
case 3:
break;
default:
break;
}
return dataPacket;
}
/**
* 图像数据包封装
*/
// public byte[] generateImagePacket(byte [] imgInfo,
public void run()
{
try
{
//以流形式读取图片
String filePath="C:\\pic02.jpg";
File f=new File(filePath);
// FileInputStream fis=new FileInputStream(f);
// DataInputStream dis=new DataInputStream(fis);
//图像数据格式封装
byte[] b3=getBytesFromFile(f);
System.out.println(b3[0]);
System.out.println(b3[1]);
byte[] bPath=filePath.getBytes();
byte[] bfileName=new byte[51];
System.arraycopy(bPath, 0, bfileName, 0, bPath.length);
byte[] bImg=new byte[b3.length+51];//加上文件名
System.arraycopy(b3, 0, bImg, 51, b3.length);
System.arraycopy(bfileName, 0, bImg, 0, bfileName.length);
byte[] imagePacket=generateDataPacket(bImg,"上海市第三研究所车辆监控基站",
(short)1000,bImg.length+37,(byte)2);
//包装成流形式
System.out.println(imagePacket.length );
System.out.println("图像文件zuihou字节:"+imagePacket[88+24853]);
for(int i=0;ib3
DataInputStream dis=new DataInputStream(bais);
//加密
//组装
// for(int i=0;i<b3.length;i++)
// {
// System.out.println(b3);
// count++;
// }
System.out.println("图像字节数组长度为: "+b3.length);
System.out.println(b3[5]);
//创建网络输出流并提供数据包装器
os=s.getOutputStream();
OutputStream ops=new DataOutputStream(new BufferedOutputStream(os));
//创建文件读取缓冲区
byte[] buf=new byte[1024];
int num=dis.read(buf);
while(num!=(-1))
{//是否读完文件
ops.write(buf,0,num);//把文件数据写出网络缓冲区
ops.flush();//刷新缓冲区把数据写往客户端
num=dis.read(buf);//继续从文件中读取数据
}
// fis.close();
ops.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public static void main(String[] args) throws Exception
{
new CommClient().start();
}
/**
* long与byte之间转换
* @param n
* @return
*/
public byte[] LongToByte (long n)
{
ByteArrayOutputStream baos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(baos);
try
{
dos.writeLong(n);
}
catch(IOException e)
{
e.printStackTrace();
}
byte [] buf=baos.toByteArray();
return buf;
}
public long ByteToLong(byte [] buf)
{
long n=0L;
ByteArrayInputStream bais=new ByteArrayInputStream(buf);
DataInputStream dis=new DataInputStream(bais);
try
{
n=dis.readLong();
}
catch(Exception e)
{
e.printStackTrace();
}
return n;
}
/**
* int与byte之间转换
* @param n
* @return
*/
public byte[] IntToByte (int n)
{
ByteArrayOutputStream baos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(baos);
try
{
dos.writeInt(n);
}
catch(IOException e)
{
e.printStackTrace();
}
byte [] buf=baos.toByteArray();
return buf;
}
public long ByteToInt(byte [] buf)
{
int n=0;
ByteArrayInputStream bais=new ByteArrayInputStream(buf);
DataInputStream dis=new DataInputStream(bais);
try
{
n=dis.readInt();
}
catch(Exception e)
{
e.printStackTrace();
}
return n;
}
/**
* short与byte之间转换
* @param buf
* @return
*/
public byte[] ShortToByte (short s)
{
ByteArrayOutputStream baos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(baos);
try
{
dos.writeShort(s);
}
catch(IOException e)
{
e.printStackTrace();
}
byte [] buf=baos.toByteArray();
return buf;
}
public short ByteToShort(byte [] buf)
{
short s=0;
ByteArrayInputStream bais=new ByteArrayInputStream(buf);
DataInputStream dis=new DataInputStream(bais);
try
{
s=dis.readShort();
}
catch(Exception e)
{
e.printStackTrace();
}
return s;
}
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/93928/showart_1967644.html |
|