Because the default JSP template used in AppFuse is Sitemesh, all requests are decorated by Sitemesh by default, so I have to clicking the file link, but I don't get the correct format when downloading the file directly, get one HTML file, even if the type of download file is developed: if: Application / Msexcel or other types are not, I analyze because I first wrote something in the output stream, and later wash away by Sitemesh using the GetWriter method, So get the default web file, I toss for 2 days, painless, just solved this problem, the solution is as follows:
1. Place the JSP of the download file in a special directory, I am placed in the download directory, which can distinguish the file that needs to be decorated by Sitemesh and do not need to be decorated by it.
2. Configure Sitemesh's decorators.xml:
exclude>
decorators>
This configuration indicates that the file under Download does not need to be decorated by Sitemesh.
My download file page code is the test code, and it can be formally used for a little modification. The code is as follows:
<% @ Page Import = "java.io. *, java.net. *"%> <%
Try {
// Get the virtual path
String fn = "attachment; filename = a.xls"; / / must be changed to Unicode encoded strings
System.out.println (fn);
// write the title, write the content in the output stream
Response.setHeader ("Content-Disposition", New String (Fn.getbytes ("GB2312"),
"ISO-8859-1")))
CreateOutput (response.getoutputStream (), "c: //ltf.xls");
} catch (exception EE) {
Ee.PrintStackTrace ();
}
%> <%!
Public void createoutput (OutputStream out, string realpath) throws oException {
INT B;
BufferedInputStream M_INPUT =
New BufferedInputStream (New FileInputStream (RealPath);
While ((b = m_input.read ())! = -1) {
Out.write (b);
}
m_input.close ();
Out.flush ();
Out.close ();
}
%>
<%
// Get the file name and path
String filename = Request.getParameter ("zipfilename");
// String jsppath = ""; getServletConfig (). GetServletContext (). GetRealPath ("")
String filepath = "c: //ltf.xls"; //jsppath.substring (0, jsppath.lastindexof (")) " report // "; system.out.println (" ----- -------- ================== " FilePath);
Java.io.fileinputStream FileInputStream = NULL;
//out.println ;filepath;
/ / Set the response header and download saved file name
Response.setHeader ("Content-Disposition",
"attachment; filename =" "a.xls");
Response.setContentType ("Application / MSExcel");
/ / Open the flow information of the specified file
Try {
FileInputStream =
New java.io.fileinputstream (filepath);
// Write out information
INT I;
While ((i = fileInputstream.read ())! = -1) {
Out.write (i);
}
FileInputStream.Close ();
Out.close ();
System.out.println ("--- OK -------------");
} catch (exception EE) {
Ee.PrintStackTrace ();
}
-%>