# 一、編解碼技術,java序列化技術
第一進行網絡傳輸
第二對象持久化
# 二、主流的編解碼框架
JBoss的Marshalling包
goole的Protobuf
基于Protobuf的Kyro
MessagePack框架
# 三、 在使用client和service分別部署的是否需要注意的問題
序列化的是否需要注意包名和類名必須一致
# 四、Mashshalling使用
public final class MarshallingCodeCFactory {
/**
* 創建Jboss Marshalling解碼器MarshallingDecoder
* @return MarshallingDecoder
*/
public static MarshallingDecoder buildMarshallingDecoder() {
//首先通過Marshalling工具類的精通方法獲取Marshalling實例對象 參數serial標識創建的是java序列化工廠對象。
final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial");
//創建了MarshallingConfiguration對象,配置了版本號為5
final MarshallingConfiguration configuration = new MarshallingConfiguration();
configuration.setVersion(5);
//根據marshallerFactory和configuration創建provider
UnmarshallerProvider provider = new DefaultUnmarshallerProvider(marshallerFactory, configuration);
//構建Netty的MarshallingDecoder對象,倆個參數分別為provider和單個消息序列化后的最大長度
MarshallingDecoder decoder = new MarshallingDecoder(provider, 1024 * 1024 * 1);
return decoder;
}
/**
* 創建Jboss Marshalling編碼器MarshallingEncoder
* @return MarshallingEncoder
*/
public static MarshallingEncoder buildMarshallingEncoder() {
final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial");
final MarshallingConfiguration configuration = new MarshallingConfiguration();
configuration.setVersion(5);
MarshallerProvider provider = new DefaultMarshallerProvider(marshallerFactory, configuration);
//構建Netty的MarshallingEncoder對象,MarshallingEncoder用于實現序列化接口的POJO對象序列化為二進制數組
MarshallingEncoder encoder = new MarshallingEncoder(provider);
return encoder;
}
}
## 五、壓縮與解壓縮
public static byte[] gzip(byte[] data) throws Exception{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(bos);
gzip.write(data);
gzip.finish();
gzip.close();
byte[] ret = bos.toByteArray();
bos.close();
return ret;
}
public static byte[] ungzip(byte[] data) throws Exception{
ByteArrayInputStream bis = new ByteArrayInputStream(data);
GZIPInputStream gzip = new GZIPInputStream(bis);
byte[] buf = new byte[1024];
int num = -1;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((num = gzip.read(buf, 0 , buf.length)) != -1 ){
bos.write(buf, 0, num);
}
gzip.close();
bis.close();
byte[] ret = bos.toByteArray();
bos.flush();
bos.close();
return ret;
}
# 六、Netty文件傳輸
1.壓縮文件放入到字節封裝好的序列發類中
2.然后通過類進行傳輸給服務器。
3.服務器解析然后獲取到對象,然后解壓圖片
# 七、java使用過程中獲取系統的一些信息
System.out.println("java版本號:" + System.getProperty("java.version")); // java版本號
System.out.println("Java提供商名稱:" + System.getProperty("java.vendor")); // Java提供商名稱
System.out.println("Java提供商網站:" + System.getProperty("java.vendor.url")); // Java提供商網站
System.out.println("jre目錄:" + System.getProperty("java.home")); // Java,哦,應該是jre目錄
System.out.println("Java虛擬機規范版本號:" + System.getProperty("java.vm.specification.version")); // Java虛 擬機規范版本號
System.out.println("Java虛擬機規范提供商:" + System.getProperty("java.vm.specification.vendor")); // Java虛 擬機規范提供商
System.out.println("Java虛擬機規范名稱:" + System.getProperty("java.vm.specification.name")); // Java虛擬機 規范名稱
System.out.println("Java虛擬機版本號:" + System.getProperty("java.vm.version")); // Java虛擬機版本號
System.out.println("Java虛擬機提供商:" + System.getProperty("java.vm.vendor")); // Java虛擬機提供商
System.out.println("Java虛擬機名稱:" + System.getProperty("java.vm.name")); // Java虛擬機名稱
System.out.println("Java規范版本號:" + System.getProperty("java.specification.version")); // Java規范版本號
System.out.println("Java規范提供商:" + System.getProperty("java.specification.vendor")); // Java規范提供商
System.out.println("Java規范名稱:" + System.getProperty("java.specification.name")); // Java規范名稱
System.out.println("Java類版本號:" + System.getProperty("java.class.version")); // Java類版本號
System.out.println("Java類路徑:" + System.getProperty("java.class.path")); // Java類路徑
System.out.println("Java lib路徑:" + System.getProperty("java.library.path")); // Java lib路徑
System.out.println("Java輸入輸出臨時路徑:" + System.getProperty("java.io.tmpdir")); // Java輸入輸出臨時路徑
System.out.println("Java編譯器:" + System.getProperty("java.compiler")); // Java編譯器
System.out.println("Java執行路徑:" + System.getProperty("java.ext.dirs")); // Java執行路徑
System.out.println("操作系統名稱:" + System.getProperty("os.name")); // 操作系統名稱
System.out.println("操作系統的架構:" + System.getProperty("os.arch")); // 操作系統的架構
System.out.println("操作系統版本號:" + System.getProperty("os.version")); // 操作系統版本號
System.out.println("文件分隔符:" + System.getProperty("file.separator")); // 文件分隔符
System.out.println("路徑分隔符:" + System.getProperty("path.separator")); // 路徑分隔符
System.out.println("直線分隔符:" + System.getProperty("line.separator")); // 直線分隔符
System.out.println("操作系統用戶名:" + System.getProperty("user.name")); // 用戶名
System.out.println("操作系統用戶的主目錄:" + System.getProperty("user.home")); // 用戶的主目錄
System.out.println("當前程序所在目錄:" + System.getProperty("user.dir")); // 當前程序所在目錄