第一步:导入maven依赖
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
第二步:编写代码
@Controller
public class DownController {
//文件下载
@RequestMapping("/down")
public void downLoad(HttpServletRequest request, HttpServletResponse response) throws IOException {
//文件下载 第一步:就是找到你要下载的文件路径
String realPath = request.getServletContext().getRealPath("/static/img/time1.jpg");
response.setHeader("Content-Disposition","attachment;filename=time1.jpg");
//获取要下载的流
FileInputStream is = new FileInputStream(realPath);
//将这个输入流写给浏览器
ServletOutputStream os = response.getOutputStream();
//输入流写给输出流 就是将获取的这个流交给输出流,
IoUtil.copy(is,os);
is.close();
os.close();
}
第三步:编写jsp页面
<a href="${pageContext.request.contextPath}/down">文件下载</a>
第四步:测试