downloaded txt file become shrinked
I am trying to write a txt file with my servlet and send it to my desktop app. The desktop app saves the file on local disk. There is no problem with downloading big binary files but txt files which are small miss final characters.
For example, servlet sends txt file whose length is 523KB, but when I save it on my desktop app the file length is 496KB?
Here is the servlet code:
final int BUFFER_SIZE = 4096;
FileInputStream in = new FileInputStream(file);
OutputStream out = response.getOutputStream();
byte buffer[] = new byte[BUFFER_SIZE];
for (int nread = 0; (nread = in.read(buffer)) != -1;) {
out.write(buffer, 0, nread);
}
out.flush();
out.close();
in.close();
And here is the desktop app code (I use HttpClient 4):
response = httpclient.execute(httppost);
resEntity = response.getEntity();
InputStream in = resEntity.g开发者_运维技巧etContent();
in = new CipherInputStream(in, decipher);//maybe the aes block missing here...
FileOutputStream out= new FileOutputStream(path);
byte[] buffer = new byte[4096];
int numRead = 0;
while ((count = in.read(buffer)) != -1) {
out.write(buffer, 0, count);
}
out.flush();
out.close();
And the decipher defined the same as for encryping...:
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
key = kgen.generateKey();
byte[] ivar = new byte[]
{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
};
AlgorithmParameterSpec params = new IvParameterSpec(ivar );
dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
dcipher.init(Cipher.DECRYPT_MODE, key, params );
Please help me to understand why I lose some characters of text files?
The servlet example code is writing bytes directly to the output stream, but the desktop app sample code is decrypting what it reads. If the desktop is decrypting data that is not encrypted, the results could be unpredictable.
精彩评论