下载的txt文件变得收缩 [英] downloaded txt file become shrinked

查看:159
本文介绍了下载的txt文件变得收缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用我的servlet编写一个txt文件并发送到我的桌面应用程序。桌面应用程序将文件保存在本地磁盘上。下载大二进制文件,但是txt文件是小错误的最终字符没有问题。

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.

例如,servlet发送长度为523KB的txt文件,但是当我保存我的桌面应用程序的文件长度是496KB?

For example, servlet sends txt file whose length is 523KB, but when I save it on my desktop app the file length is 496KB?

这是servlet代码:

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();

这里是桌面应用程序代码(我使用HttpClient 4):

And here is the desktop app code (I use HttpClient 4):

response = httpclient.execute(httppost);
resEntity = response.getEntity();
InputStream in = resEntity.getContent();
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?

推荐答案

servlet示例代码直接将字节写入输出流,但桌面应用程序示例代码正在解密它所读取的内容。如果桌面正在解密未加密的数据,则结果可能是不可预测的。

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.

这篇关于下载的txt文件变得收缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆