首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 互联网 >

DownloadAction上载及上载次数统计加刷新详解

2012-07-08 
DownloadAction下载及下载次数统计加刷新详解struts中downloadAction下载详解,包括统计下载次数,刷新页面

DownloadAction下载及下载次数统计加刷新详解

struts中downloadAction下载详解,包括统计下载次数,刷新页面上下载次数
这是Action代码package action;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.actions.DownloadAction.ResourceStreamInfo;import org.apache.struts.actions.DownloadAction.StreamInfo;import com.huatf.agitar.service.HelpproductService;/** * 文档下载类 * @author pxAgitar * */public class DownAction{      //services 类    private HelpproductService helpproductService = new HelpproductService();      //地址    private String path;      //文件路径    private String filename;       protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form,   HttpServletRequest request, HttpServletResponse response)   throws Exception  {    String paths = path+filename;    response.setHeader("content-disposition", "attachment; filename="+ filename);    ResourceStreamInfo rsi = new ResourceStreamInfo("application/file",request.getSession().getServletContext(),paths);    return rsi; } public ActionForward execute(ActionMapping mapping, ActionForm form,   HttpServletRequest request, HttpServletResponse response)   throws Exception  {     path = request.getParameter("path");        filename = request.getParameter("file");       StreamInfo info = getStreamInfo(mapping, form, request, response);          String contentType = info.getContentType();          InputStream stream = info.getInputStream();     ServletOutputStream fs = response.getOutputStream();          try     {             response.setContentType(contentType);              copy(stream, fs);            }catch(Exception e)     {      //用户点击取消下载后,还回为空,不记录下载次数      return null;     }     finally      {            if (stream != null)          {            stream.close();         }     }     //向后台添加下载次数     helpproductService.addCount(filename);     //必须还回空,不然会报错:java.lang.IllegalStateException: Cannot forward after response has been committed      return null; } public int copy(InputStream input, OutputStream output)      throws IOException {     byte[] buffer = new byte[getBufferSize()];     int count = 0;     int n = 0;     while (-1 != (n = input.read(buffer)))      {      output.write(buffer, 0, n);         count += n;     }     if(count !=0)     {      output.flush();      input.close();     }     //这里还回的是文件大小     return count;   }   protected int getBufferSize()   {     return 4096;   }}配置文件:  <action path="/download" scope="request" type="action.DownAction"/>JSP代码:  <a href="download.do?path=${helppro.docurl }&file=${helppro.docname }" onclick="show();" >点击下载</a> onclick=show();页面刷新下载次数 function show(){    setTimeout("location.href='当前页面url'",5000);} 

?

热点排行