当我尝试从服务器上传文件时,我使用此代码。我得到的文件大小为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();
答案 0 :(得分:0)
ByteArrayOutputStream
完全浪费时间和空间。您正在阅读附件两次,并将其写入两次。只需从附件中读取并直接写入文件即可。简化,简化。你不需要90%。