Copy codeThe Code is as follows:
/**
* Implement the file storage function
*
* @ Param text
* File Content
* @ 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;
}
Download action:
Copy codeThe Code is as follows:
/***//**
* Provides the 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 codeThe Code is as follows:
<A href = "$ {ctx}/va! Down. do "> download </a>