前言:
web开发中上传和下载是最基本的内容了。前几天写了一篇上传的案例,今天总结一下下载。
案例说明:
1.本案例是使用myeclipse编写
2.需要下载的文件存放的 /download_demo/WebRoot/WEB-INF/attachment 目录下面---这里我只是放进去了测试文件
下载文件思路:
1.得到服务器上面的文件File
2.得到输入流,将文件流化
3.得到servlet输出流,将文件已流的方式写到客户端
看懂案例中的需要的知识点:
1.servlet的配置
2.filter的配置
3.java文件的基础知识
4.java流的知识
前台页面代码:一、index.jsp页面
功能:点击按钮,进入下载文件页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML>
<html>
<head>
<base href="<%=basePath%>">
<title>download demo</title>
<meta charset="UTF-8">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.js"></script>
<script type="text/javascript">
$(function(){