各位大神好,我在学习java网络编程时想要建立既可以压缩又可以加密的流,但是在代码
执行过程中在服务器端的输入流建立时被阻塞了,代码无法继续走下去。经过调查我认为
是因为该输入流没有得到从客户端输出流发送过来的数据所以被阻塞了。虽然知道了原因
但是我本人实在是不知道怎么改这段代码,已经被困了一个星期了,这个问题还是没有解
决。如果有知道的大神请您务必帮帮忙,谢谢了。
以下是代码:
服务器端初始化:
serverSession(){
initInput();
initOutput();
}
客户端初始化:
clientSession(){
initOutput();
initInput();
}
initOutput(){
BufferedOutputStream bos = new BufferedOutputStream(soc_.getOutputStream());
String KEY = "abc_1234_ef@1234";
String DEFAULT_CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
String KEY_ALGORITHM = "AES";
SecretKeySpec skeySpec = new SecretKeySpec(KEY.getBytes(), KEY_ALGORITHM);
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
CipherOutputStream cos = new CipherOutputStream(bos, cipher);
byte[] iv = cipher.getIV();
bos.write(iv);
GZIPOutputStream gos = new GZIPOutputStream(cos);
ObjectOutputStream oos = new ObjectOutputStream(gos);
oos.flush();
gos.flush();
cos.flush();
bos.flush();
}
initInput(){
BufferedInputStream is = new BufferedInputStream(soc_.getInputStream());
byte[] iv = new byte[16];
is.read(iv);
String KEY = "abc_1234_ef@1234";
String DEFAULT_CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
String KEY_ALGORITHM = "AES";
SecretKeySpec skeySpec = new SecretKeySpec(KEY.getBytes(), KEY_ALGORITHM);
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(iv));
CipherInputStream cis = new CipherInputStream(is, cipher);
GZIPInputStream gis = new GZIPInputStream(cis);
ObjectInputStream ois = new ObjectInputStream(gis);
}