JSP 點擊鏈接后下載文件(相當于右鍵另存)功能
更新時間:2009年07月01日 23:43:35 作者:
JSP 點擊鏈接后下載文件(相當于右鍵另存)功能實現(xiàn)代碼。
復制代碼 代碼如下:
/**
* 實現(xiàn)文件另存功能
*
* @param text
* 文件內(nèi)容
* @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;
}
頁面鏈接調(diào)用:
復制代碼 代碼如下:
<a href="${ctx}/va/va!down.do" >下載</a>
相關文章
Spring 整合 Hibernate 時啟用二級緩存實例詳解
這篇文章主要介紹了Spring 整合 Hibernate 時啟用二級緩存實例詳解的相關資料,需要的朋友可以參考下2017-02-02JSP使用Common FileUpload組件實現(xiàn)文件上傳及限制上傳類型實例代碼
這篇文章主要介紹了JSP使用Common FileUpload組件實現(xiàn)文件上傳及限制上傳類型實例代碼,需要的朋友可以參考下2017-09-09