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 }" οnclick="show();" >点击下载</a>
 
οnclick=show();
页面刷新下载次数 	
function show()
{
    setTimeout("location.href='当前页面url'",5000);
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值