为什么选择ServletResponseUtil. sendFile发送文件作为页面内容吗? [英] Why ServletResponseUtil. sendFile sends a file as page content?

查看:87
本文介绍了为什么选择ServletResponseUtil. sendFile发送文件作为页面内容吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成一个文件并将其像文件一样上传到磁盘.

I want to generate a file and upload it to disk like a file.

我卸载为

ServletResponseUtil.sendFile(httpServletRequest, httpServletResponse, fileName, baos.toByteArray(), ContentTypes.APPLICATION_OCTET_STREAM);

结果,该文件不是作为文件上传到磁盘,而是作为文件上传到浏览器页面

As a result, the file is not uploaded as a file to disk, but to the browser page as

ࡱ ; V 根条目 FW 工作簿 ! SummaryInformation( D

�� ࡱ �; �� V ���� ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Root Entry �������� �FW� Workbook ������������ !� SummaryInformation( ���� D

响应头

HTTP/1.1 200 
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Cache-Control: private, no-cache, no-store, must-revalidate
Pragma: no-cache
Liferay-Portal: Liferay Community Edition Portal 7.2.0 CE GA1 (Mueller / Build 7200 / June 4, 2019)
Content-Disposition: attachment; filename="test.xls"
Content-Type: application/octet-stream;charset=UTF-8
Content-Length: 46080
Date: Tue, 24 Dec 2019 07:21:40 GMT

推荐答案

最有可能发生这种情况的原因是,您正在Portlet的操作方法或渲染方法中运行此代码. Portlet仅呈现将始终嵌入适当HTML页面中的HTML页面片段.如果通过获取HttpServletRequest和(更具体而言)HttpServletResponse来模拟,则只需将门户的呈现输出与您自己的输出混合.

That most likely happens because you're running this code within an action- or render-method of your portlet. Portlets render only fragments of HTML pages that will always be embedded in a proper HTML page. If you mock with that by obtaining the HttpServletRequest and (more specifically) HttpServletResponse, you'll just mix the portal's rendered output with your own.

您应该使用serveResource方法来托管您的实现,在那里您甚至不需要还原回HttpServletResponse,而只需将输出流式传输到RenderResponse即可.

You should use the serveResource method to host your implementation, there you don't even need to revert back to the HttpServletResponse, but can just stream your output to the RenderResponse.

即使您最终没有看到混合的HTML/XLS内容,而仅看到实际的XLS输出,也可能会干扰Liferay的SPA加载程序,您可以假定该程序知道预期的返回值,例如呈现时,它需要HTML门户页面,而不是二进制数据.当您使用serveResource方法时,情况并非如此,因为它会返回任意Mimetypes.

Even if you don't end up with mixed HTML/XLS content but see only the actual XLS output, you might interfere with Liferay's SPA loader, which you can assume knows about expected return values, e.g. when rendering, it expects a HTML portal page, not binary data. That will not be the case when you use the serveResource method, as it is expected to return arbitrary Mimetypes.

这篇关于为什么选择ServletResponseUtil. sendFile发送文件作为页面内容吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆