JSP click on the link to download the file (right key Save) function
/** *//**
* 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:
/** *//**
* 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:
<a href= "${ctx}/va/va!down.do" > Download </a>