/** *//**
* 实现文件另存功能
*
* @param text
* 文件内容
* @param filename
* 文件名称
* @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:
/** *//**
* 提供下载的方法
* @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;
}
更多信息请查看IT技术专栏