利用一个Servlet来统计文件的下载次数
package com.helloxr.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class RedirectServlet extends HttpServlet { private static final long serialVersionUID = 19999999L; Map<String, Integer> map = new HashMap<String, Integer>(); public void destroy() { map = null; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String filename = request.getParameter("filename"); if(filename != null){ int hit = map.get(filename); // 取下载次数 map.put(filename, ++hit); // 下载次数加1后保存 response.sendRedirect(request.getContextPath() + filename); // 重定向到文件 }else{ response.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>文件下载</TITLE></HEAD>"); out.println("<link rel='stylesheet' type='text/css' href='../css/style.css' />"); out.println(" <BODY>"); out.println("<fieldset align='center' style='90%'><legend>文件下载</legend><br/>"); out.println("<table width=100%>"); out.println(" <tr>"); out.println(" <td><b>文件名</b></td>"); out.println(" <td><b>下载次数</b></td>"); out.println(" <td><b>下载</b></td>"); out.println(" </tr>"); for(Entry<String, Integer> entry: map.entrySet()){ out.println("<tr>"); out.println(" <td>" + entry.getKey() + "</td>"); out.println(" <td>" + entry.getValue() + "</td>"); out.println(" <td><a href='"+ request.getRequestURI() + "?filename=" + entry.getKey() + "' target=' blank' onclick='location=locaction; '>下载</a></td>"); out.println("</tr>"); } out.println("</table>"); out.println(" </fieldset>"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } } @Override public void init() throws ServletException { map.put("/download/setup.exe", 0); map.put("/download/application.zip", 0); map.put("/download/01.mp3", 0); } }
运行效果: