Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a JSP file which creates an Excel document.

I want to dynamically set the name of the file to be downloaded.

This is how I set the file name to "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 ) ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
318 views
Welcome To Ask or Share your Answers For Others

1 Answer

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...