我们如何阅读或使用outputstream的内容 [英] How can we read or use the contents of outputstream
问题描述
我们如何阅读或使用outputstream的内容.就我而言,我使用的是具有签名的方法.
How can we read or use the contents of outputstream. In my case, I am using a method having signature.
公共静态OutputStreamcrypt解密AsStream(InputStream inputStream,字符串加密密钥)
此方法返回OutputStream.我想将OutputStream转换为String.是否可以将java.io.OutputStream的输出传递给Java中的String?
This method return OutputStream. I want get OutputStream to String. Is it possible to pipe the output from an java.io.OutputStream to a String in Java?
推荐答案
我们如何阅读或使用outputstream的内容.
How can we read or use the contents of outputstream.
通常您不能. OutputStream
是您向其写入字节的对象.通用API没有提供任何方法来取回字节.
In general you can't. An OutputStream
is an object that you write bytes to. The general API doesn't provide any way to get the bytes back.
有一些特定类型的输出流,可让您取回字节.例如:
There are specific kinds of output stream that would allow you to get the bytes back. For instance:
-
ByteArrayOutputStream
具有获取字节数组内容的方法. -
PipeOutputStream
具有相应的PipeInputStream
,您可以从中读取. - 如果您使用
FileOutputStream
写入文件,则可以通常打开FileInputStream
来读取文件内容...知道文件路径名.
- A
ByteArrayOutputStream
has a method for getting the byte array contents. - A
PipeOutputStream
has a correspondingPipeInputStream
that you can read from. - If you use a
FileOutputStream
to write to file, you can typically open aFileInputStream
to read the file contents ... provided you know the file pathname.
看着那个方法,让我惊讶的是,对于您要尝试执行的操作,方法签名是错误.如果目的是为流提供加密器,则该方法应返回 InputStream
.如果目的是在某处写入加密数据,则该方法应返回 void
并接受一个额外的参数,该参数是该方法的 OutputStream
应该写.(然后,调用者可以使用 OutputStream
子类来捕获加密的数据……如果需要的话.)
Looking at that method, it strikes me that the method signature is wrong for what you are trying to do. If the purpose is to provide an encrypter for a stream, then the method should return an InputStream
. If the purpose is to write the encrypted data somewhere, then the method should return void
and take an extra parameter that is the OutputStream
that the method should write to. (And then the caller can use an OutputStream
subclass to capture the encrypted data ... if that is what is desired.)
另一个可行的替代方法是更改方法的签名,使返回类型为 ByteArrayOutputStream
(或文件名).但这不是一个好的解决方案,因为它剥夺了调用者决定应将加密输出发送到何处的能力.
Another alternative that would "work" is to change the method's signature to make the return type ByteArrayOutputStream
(or a file name). But that is not a good solution because it takes away the caller's ability to decide where the encrypted output should be sent.
更新
关于您的解决方案
OutputStream os = AESHelper.decryptAsStream(sourceFile, encryptionKey);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos = (ByteArrayOutputStream) os;
byte[] imageBytes = baos.toByteArray();
response.setContentType("image/jpeg");
response.setContentLength(imageBytes.length);
OutputStream outs = response.getOutputStream();
outs.write(imageBytes);
那行得通,但是代码很糟糕:
That could work, but it is poor code:
-
如果
AESHelper.decryptAsStream
是您编写的方法(看起来像是!),则应将其声明为返回ByteArrayOutputStream
.
If
AESHelper.decryptAsStream
is a method that you wrote (and it looks like it is!), then you should declare it as returning aByteArrayOutputStream
.
如果已经声明它返回了 ByteArrayOutputStream
,则应将其直接分配给 baos
.
If it is already declared as returning a ByteArrayOutputStream
you should assign it directly to baos
.
无论哪种方式,都不应将 baos
初始化为新创建的立即被丢弃的 ByteArrayOutputStream
实例.
Either way, you should NOT initialize baos
to a newly created ByteArrayOutputStream
instance that immediately gets thrown away.
还值得注意的是Content-Type不正确.如果您将该响应发送给浏览器,它将尝试将加密的数据解释为图像...并失败.从理论上讲,您可以设置一个Content-Encoding头...但是没有一个可以使用.因此,最好的解决方案是将Content-Type发送为"application/octet-stream".
It is also worth noting that the Content-Type is incorrect. If you sent that response to a browser, it would attempt to interpret the encrypted data as an image ... and fail. In theory you could set a Content-Encoding header ... but there isn't one that is going to work. So the best solution is to send the Content-Type as "application/octet-stream".
这篇关于我们如何阅读或使用outputstream的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!