网络传对象
我使用 序列化对象(ByteArrayOut/InputStream;ObjectOut/InputStream)
public static byte[]objectToByte(Object object) { byte[] bytes = null; try { //object to bytearray ByteArrayOutputStream bo = new ByteArrayOutputStream(); ObjectOutputStream oo = new ObjectOutputStream(bo); oo.writeObject(object); bytes = bo.toByteArray(); bo.close(); oo.close(); } catch(Exception e) { e.printStackTrace(); } return bytes; }
public static Object byteToObject(byte[] bytes) throws Exception { java.lang.Object obj = null; //bytearray to object ByteArrayInputStream bi = new ByteArrayInputStream(bytes); ObjectInputStream oi = new ObjectInputStream(bi); obj = oi.readObject(); bi.close(); oi.close(); return obj; }
public class Message implements Serializable{ String sendName; String receiveName; int command; Date sendDate; Object content; public Message() { } public Message(String inSendName,String inReceiveName,int inCommand,Date inSendDate,Object inContent) { this.setSendName(inSendName); this.setReceiveName(inReceiveName); this.setCommand(inCommand); this.setSendDate(inSendDate); this.setContent(inContent); }//后面get,set就不贴了