JAVA全系列 教程
3762个小节阅读:7094.8k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
将文件上传到服务器后,有时我们需要让用户下载上传的文件,下载分为两步,第一步查询可以下载的文件,第二步选中文件进行下载。首先我们编写查询所有可下载文件的功能:
编写控制器方法,查询所有可下载的文件,并跳转到下载页面
xxxxxxxxxx
// 查询可下载的文件
@RequestMapping("/showFiles")
public String showFileDown(Model model){
// 1.获取下载文件的路径集合。注:跨服务器上传中,网络路径无法获取文件列表。
String path = "E:\\springmvc\\mvc_demo1\\upload";
File file = new File(path);
String[] files = file.list();
// 2.将路径放入Model中,跳转到JSP页面
model.addAttribute("files",files);
return "download";
}
添加JSTL依赖
xxxxxxxxxx
<!-- JSTL -->
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
<version>2.0.0</version>
</dependency>
编写下载页面
xxxxxxxxxx
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>下载</title>
</head>
<body>
<h3>文件下载</h3>
<%-- 遍历文件集合 --%>
<c:forEach items="${files}" var="file">
<a href="/download?fileName=${file}">${file}</a><br/>
</c:forEach>
</body>
</html>