JSP click on the link to download the file (right key Save) function

Source: Internet
Author: User
Tags key return string
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>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.