Copy Code code as follows:
/**
* Implement file Save function
*
* @param text
* Contents of the file
* @param fileName
* File name
* @return
*/
Protected string RenderFile (string text, String fileName)
Throws IOException
{
Response.AddHeader ("Content-disposition", "attachment; Filename= "
+ FileName);
Response.setcontenttype ("Application/octet-stream");
Response.setcharacterencoding ("GB2312");
Response.getwriter (). write (text);
Response.flushbuffer ();
Response.getwriter (). Close ();
return null;
}
Action to download:
Copy Code code as follows:
/** *//**
* Provide a download method
* @return
*/
Public String Down ()
{
String dir = GetFullPath () + "/upload/file/";
Try
{
if (! Fileutils.exists (dir))
{
New File (dir). Mkdirs ();
}
Random r = new Random (System.currenttimemillis ());
Integer randomint = R.nextint ();
This.renderfile ("Test content:" + randomint,randomint + ". txt");
}
catch (IOException E)
{
E.printstacktrace ();
This.rendertext (E.getmessage ());
}
return null;
}
Page link Call:
Copy Code code as follows:
<a href= "${ctx}/va/va!down.do" > Download </a>