将输入流下载为文件(附件)

时间:2017-04-17 10:14:56

标签: java servlets download

我有数据进入输入流。我有一个servlet,我可以获取数据。我想将收到的数据下载为文件。 这是我到目前为止提出的代码,但没有成功。任何帮助都会很棒!谢谢!

final InputStream inStream = new BufferedInputStream(fetchFile.getObjectContent());
byte[] bytes = IOUtils.toByteArray(inStream);
ServletOutputStream out = response.getOutputStream();
InputStream in = new ByteArrayInputStream(bytes);
response.setContentType("application/octet-stream");
response.setContentLength(bytes.length);
response.setHeader("Content-Disposition","attachment;filename=\"" + filename + "\"");
byte[] outputByte = new byte[bytes.length];
while(in.read(outputByte, 0, bytes.length) != -1)
{
    out.write(outputByte, 0, bytes.length);
}
in.close();
out.flush();
out.close();

0 个答案:

没有答案