上传文件使用FileOutputStream错误.不正确的大小



当我尝试从服务器上传文件时,我使用这段代码。我得到的文件大小为500kb,原来的文件大小约300kb。我做错了什么?

attachmentContent = applicationApi.getApplicationAttachmentContent(applicationame);  
InputStream in = new BufferedInputStream(attachmentContent.getAttachmentContent().getInputStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n=0;
while (-1 !=(n=in.read(buf)))
{
    out.write(buf,0,n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
File transferredFile = new File(attachmentName);
FileOutputStream outStream = new FileOutputStream(transferredFile);
attachmentContent.getAttachmentContent().writeTo(outStream);
outStream.write(response);
outStream.close();

简化。相同的结果:

File transferredFile = new File(attachmentName);
FileOutputStream outStream = new FileOutputStream(transferredFile);
attachmentContent.getAttachmentContent().writeTo(outStream);
outStream.close();

ByteArrayOutputStream完全是浪费时间和空间。你读了两遍附件,也写了两遍。只需从附件中读取并直接写入文件。简化,简化。你不需要90%的内容

最新更新