Java 我们如何读取或使用 outputstream 的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18029609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can we read or use the contents of outputstream
提问by Amit Kumar
How can we read or use the contents of outputstream. In my case, I am using a method having signature.
我们如何读取或使用 outputstream 的内容。就我而言,我使用的是具有签名的方法。
public static OutputStream decryptAsStream(InputStream inputStream, String encryptionKey)
公共静态输出流解密AsStream(输入流输入流,字符串加密密钥)
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。我想将 OutputStream 转换为 String。是否可以将 java.io.OutputStream 的输出通过管道传输到 Java 中的字符串?
回答by NINCOMPOOP
If you got the OutputStream
from the response
, you can write the contents of the OutputStream
to the response which will be sent back to the browser . Sample code :
如果您OutputStream
从 获得response
,您可以将 的内容写入OutputStream
将被发送回浏览器的响应。示例代码:
OutputStream outStream = response.getOutputStream();
response..setHeader("Content-Disposition", "attachment; filename=datafile.xls");
response.setContentType("application/vnd.ms-excel");
byte[] buf = new byte[4096];
int len = -1;
while ((len = inStream.read(buf)) != -1) {
outStream.write(buf, 0, len);
}
outStream.flush();
outStream.close();
Read this : Content-Disposition:What are the differences between “inline” and “attachment”?
阅读本文:Content-Disposition:“内联”和“附件”有什么区别?
In your case the method definition is :
在您的情况下,方法定义是:
public static OutputStream decryptAsStream(InputStream inputStream,
String encryptionKey)
So you can get the OutputStream
from that method as :
所以你可以OutputStream
从那个方法中得到:
OutputStream os = ClassName.decryptAsStream(inputStream,encryptionKey);
And then use the os
.
然后使用os
.
回答by Ushani
You can do something like following :
您可以执行以下操作:
OutputStream output = new OutputStream()
{
private StringBuilder string = new StringBuilder();
@Override
public void write(int x) throws IOException {
this.string.append((char) x );
}
public String toString(){
return this.string.toString();
}
};
回答by UVM
Mainly you can use outputstreams
to send the file contents as @The New Idiot mentioned. .pdf files, zip file, image files etc.
主要是您可以outputstreams
像@The New Idiot 提到的那样发送文件内容。.pdf 文件、zip 文件、图像文件等。
In such scenarios, get the output stream of your servlet and to that write the contentes
在这种情况下,获取 servlet 的输出流并写入内容
OutputStream outStream = response.getOutputStream();
FileInputSteram fis = new FileInputStream(new File("abc.pdf));
byte[] buf = new byte[4096];
int len = -1;
while ((len = fis.read(buf)) != -1) {
outStream .write(buf, 0, len);
}
response.setContentType("application/octect");(if type is binary) else
response.setContentType("text/html");
outStream .flush();
outStream.close();
回答by Stephen C
How can we read or use the contents of outputstream.
我们如何读取或使用 outputstream 的内容。
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.
一般来说你不能。AnOutputStream
是您向其写入字节的对象。通用 API 不提供任何方式来取回字节。
There are specific kinds of output stream that would allow you to get the bytes back. For instance:
有特定类型的输出流可以让您取回字节。例如:
- 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 typicallyopen aFileInputStream
to read the file contents ... provided you know the file pathname.
- A
ByteArrayOutputStream
有一种获取字节数组内容的方法。 - A
PipeOutputStream
有一个对应的PipeInputStream
,您可以从中读取。 - 如果您使用 a
FileOutputStream
写入文件,您通常可以打开 aFileInputStream
来读取文件内容……前提是您知道文件路径名。
Looking at that method, it strikes me that the method signature is wrongfor 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.)
看着那个方法,我觉得方法签名对于你想要做的事情是错误的。如果目的是为流提供加密器,则该方法应返回一个InputStream
. 如果目的是将加密数据写入某处,则该方法应返回void
并采用OutputStream
该方法应写入的额外参数。(然后调用者可以使用OutputStream
子类来捕获加密数据......如果这是需要的话。)
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.
另一种“有效”的替代方法是更改方法的签名以生成返回类型ByteArrayOutputStream
(或文件名)。但这不是一个好的解决方案,因为它剥夺了调用者决定加密输出应该发送到哪里的能力。
UPDATE
更新
Regarding your solution
关于你的解决方案
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:
这可以工作,但代码很差:
If
AESHelper.decryptAsStream
is a method that you wrote (and it looks like it is!), then you should declare it as returning aByteArrayOutputStream
.If it is already declared as returning a
ByteArrayOutputStream
you should assign it directly tobaos
.Either way, you should NOT initialize
baos
to a newly createdByteArrayOutputStream
instance that immediately gets thrown away.
如果
AESHelper.decryptAsStream
是您编写的方法(看起来确实如此!),那么您应该将其声明为返回一个ByteArrayOutputStream
.如果它已声明为返回 a
ByteArrayOutputStream
,则应将其直接分配给baos
.无论哪种方式,您都不应该初始化
baos
为ByteArrayOutputStream
立即被丢弃的新创建的实例。
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".
还值得注意的是 Content-Type 不正确。如果您将该响应发送到浏览器,它会尝试将加密数据解释为图像……并失败。理论上你可以设置一个 Content-Encoding 标头......但没有一个可以工作。所以最好的解决方案是将 Content-Type 作为“application/octet-stream”发送。