Java 如何在 JSP 中动态设置标头值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3417895/
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 to dynamically set header value in JSP
提问by jeph perro
I have a JSP file which creates an Excel document.
我有一个创建 Excel 文档的 JSP 文件。
I want to dynamically set the name of the file to be downloaded.
我想动态设置要下载的文件的名称。
This is how I set the file name to "test.xsl":
这就是我将文件名设置为“test.xsl”的方式:
<% response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment; filename=" + "test.xsl" );
%>
How can I set the file name to be test-${today's date}.xsl ( i.e. test-20100805.xsl ) ?
如何将文件名设置为 test-${今天的日期}.xsl(即 test-20100805.xsl)?
采纳答案by Shawn D.
String fname = MessageFormat.format(
"test-{0,date,yyyyMMdd}.xsl", new Object [] { new Date() } );
response.setHeader("Content-Disposition","attachment; filename=" + fname );
I think this should work for you.
我认为这应该对你有用。
The text in the braces tells the MessageFormat
class to insert value 0
from the given array, format it as a date
using the format yyyyMMdd
(e.g. 20161231
for Dec 31st 2016).
大括号中的文本告诉MessageFormat
类0
从给定的数组中插入值,将其格式化为date
使用格式yyyyMMdd
(例如20161231
2016 年 12 月 31 日)。