The first step is to write an HTML file of your own (write the file template according to your own needs)
The second part uses STRUTS2 as action
Public String Downloadfiles () {try {httpservletrequest request = Servletactioncontext.getrequest ();
HttpServletResponse response = Servletactioncontext.getresponse ();
Solve the Chinese garbled problem Response.setcontenttype ("Application/x-download;charset=utf-8"); /** Read template file content/FileInputStream input = new FileInputStream (Request. Getrealpath ("") + "/web-inf/tem
P.html "));
int bytelength = input.available ();
String title = "Dream of the Three Kingdoms download any format file"; String content = "Test .... ... ..... ..... ..... ...... ...... ...
";
String editer = "Three faithful fans of the Dream";
byte[] bytes = new Byte[bytelength];
Input.read (bytes);
Input.close ();
String templatecontent = new string (bytes);
System.out.println ("template file content = = =" + TemplateContent);
TemplateContent = Templatecontent.replaceall ("# # #title ###", title);
TemplateContent = Templatecontent.replaceall ("# # #content ###", content); TemplateContent = TemplateContent. ReplaceAll ("# # #author ###", editer);//replace the corresponding place in the template System.ouT.print ("Replaced file contents" + TemplateContent); String name = "Three Kingdoms of Dreams. html";//download file name byte[] outputcontent = templatecontent.getbytes ()//download file contents Response.setcontentty
PE ("Application/x-msdownload");
Response.setheader ("Content-disposition", "attachment;filename=" + New String (Name.getbytes ("GBK"), "iso-8859-1")); Response.getoutputstream (). write (outputcontent);
Write file Response.getoutputstream (). Flush ();
Response.getoutputstream (). Close ();
catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();
catch (Unsupportedencodingexception e) {//TODO auto-generated catch block E.printstacktrace ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
return null; }
Finally we can download the file by accessing the action
The above information is expected to help you.