Servlet实现web站点文件下载功能示例
2018-07-20 来源:open-open
1、首先创建一个SharedFilesServlet用于返回一个文件分享界面
package com.hyman.view; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class SharedFilesServlet */ public class SharedFilesServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public SharedFilesServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html; charset=utf-8"); PrintWriter out = response.getWriter(); out.println("<script type='text/javascript' language='javascript'>" + "function fileDown(obj){" + "var resourceName = '';" + "var buttonId = obj.getAttribute('id');" + "var eleName = buttonId.substring(0,3) + 'File';" + "resourceName = document.getElementsByName(eleName)[0].value;" + "var url = '/SharingSite/DownFileServlet?fileName=' + resourceName;" + "window.location.href=url;" + "}" + "</script>"); out.println("<h1>文件共享网站</h1>"); out.println("mp3资源:<input type='text' name='mp3File' /> <input type='submit' value ='点击下载' id='mp3Down' onclick='fileDown(this)' /><br/>"); out.println("avi资源:<input type='text' name='aviFile' /> <input type='submit' value ='点击下载' id='aviDown' onclick='fileDown(this)' /><br/>"); out.println("jpg资源:<input type='text' name='jpgFile' /> <input type='submit' value ='点击下载' id='jpgDown' onclick='fileDown(this)' /><br/>"); out.println("txt资源:<input type='text' name='txtFile' /> <input type='submit' value ='点击下载' id='txtDown' onclick='fileDown(this)' /><br/>"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doGet(request, response); } }
2、然后创建一个控制器,来判断是否盗链,以及处理文件下载逻辑:
package com.hyman.controller; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class DownFileServlet */ public class DownFileServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public DownFileServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html; charset=utf-8"); String referer = request.getHeader("Referer"); if (referer == null || !referer.startsWith("http://localhost:8080/SharingSite/")) { response.sendRedirect("/SharingSite/AntiPirateLink"); } else { String fileName = request.getParameter("fileName"); response.setHeader("Content-Disposition", "attachment; filename="+fileName); String path = this.getServletContext().getRealPath("/resources/"+fileName); FileInputStream fis = null; OutputStream os = null; try { fis = new FileInputStream(new File(path)); byte buff[] = new byte[1024]; int len = 0; os = response.getOutputStream(); while ((len = fis.read(buff)) > 0) { os.write(buff, 0, len); } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(); } finally { if (os != null) os.close(); if (fis != null) fis.close(); } } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doGet(request, response); } }
3、创建一个AntiPirateLink返回盗链提醒界面,并在10秒后自动刷新跳转到SharedFilesServlet
package com.hyman.view; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class AntiPirateLink */ public class AntiPirateLink extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public AntiPirateLink() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html; charset=utf-8"); PrintWriter out = response.getWriter(); out.println("<script type='text/javascript' language='javascript'>" + "var i = 10;" + "function showTime(){" + "if(i > 0) {document.getElementById('time').innerHTML = '请不要盗链下载... ' + i +' 秒后自动跳转到下载首页...';" + "i--;}" + "}" + "setInterval('showTime()',1000);" + "</script>"); out.println("<span id='time'></span>"); response.setHeader("Refresh", "10; url=/SharingSite/SharedFilesServlet"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doGet(request, response); } }
【分析】文件下载部分实现步骤:
【分析】文件下载部分实现步骤:1、response.setHeader("Content-Disposition", "attachment; filename="+fileName);在http回复消息头中的Content-Disposition中指明下文件的文件名;
2、调用this.getServletContext().getRealPath("/resources/"+fileName);函数获取到文件的全路径;
3、使用文件流FileInputStream将文件读入到服务器内存,重要要用到缓存处理,然后再调用response的getOutputStream()方法获得向客户端写文件的输出文件流;
来自: http://www.cnblogs.com/page-of-Hyman/p/4321523.html
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:通过java来获取系统的信息
最新资讯
热门推荐