注: 开发环境搭建请参照 https://my.oschina.net/u/2490316/blog/781799
上传的模板请参照 https://my.oschina.net/u/2490316/blog/803030
1. 下载的jsp文件内容
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>download</title>
<script type="text/javascript" src="${webServer}/resource/js/jquery-3.1.1.min.js"></script>
</head>
<body>
<!-- 第一种下载方式,直接文件在项目中的路径 -->
<div style="border: 1px solid beige;">
<c:forEach items="${listFile}" var="file">
<span>${file.type}----</span><a href="javascript:void(0);">${file.name}</a>
<span>-----</span><a href="${file.path}">下载</a>
<br />
</c:forEach>
</div>
<a href="${webServer}/resource/fail.doc">导入模板下载</a>
<h1>-------------------------------------------------------------------------------</h1>
<hr />
<!-- 第二种下载方式,经过后台处理进行下载 -->
<div style="border: 1px solid beige;">
<c:forEach items="${listFile}" var="file" varStatus="status">
<span>${file.type}----</span><a href="javascript:void(0);">${file.name}</a>
<span>-----</span>
<button class="downloadbutton" data-name="${file.name}">下载</button>
<br />
</c:forEach>
<span>file----</span><a href="javascript:void(0);">test_fail.txt</a>
<span>-----</span>
<button class="downloadbutton" data-name="test_fail.txt">下载</button>
<form name="downloadform" action="downloadFile" method="post">
<input type="hidden" name="fileName" />
</form>
</div>
<script>
$(".downloadbutton").click(function(){
$("input[name='fileName']").val($(this).data("name"));
$('form').submit();
});
</script>
</body>
</html>
2.controller 文件处理内容
package com.study.controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.study.pojo.FileProperty;
@Controller
public class downloadController {
@Value("#{settings.webServer}")
private String webPath;
//获取目录下所有文件名称
@RequestMapping("listFile")
public String listFile(Model model, HttpServletRequest request, HttpServletResponse response){
List<FileProperty> listFile = new ArrayList<>();
String path = request.getServletContext().getRealPath("/")+"uploadFiles";
File dir = new File(path);
if(!dir.exists()){
return "fail";
}
File[] file = dir.listFiles();
for(int i=0; i<file.length; i++){
File temp = file[i];
FileProperty fileProperty = new FileProperty();
fileProperty.setName(temp.getName());
fileProperty.setPath(webPath+"/uploadFiles/"+temp.getName());
if(temp.isDirectory()){
fileProperty.setType("directory");
} else if(temp.isFile()){
fileProperty.setType("file");
}
listFile.add(fileProperty);
}
model.addAttribute("listFile",listFile);
return "download";
}
//下载文件的后台处理方式
@RequestMapping("downloadFile")
public void downloadFile(HttpServletRequest request, HttpServletResponse response) throws Exception{
String fileName = request.getParameter("fileName");
System.out.println(fileName);
//在上传路径中找对应的文件
String path = request.getServletContext().getRealPath("/")+"uploadFiles";
File dir = new File(path);
File[] file = dir.listFiles();
File getFile = null;
for(int i=0; i<file.length; i++){
if(file[i].getName().equals(fileName)){
getFile = file[i];
break;
}
}
if(getFile == null){
request.setAttribute("message", "您要下载的资源已被删除!!");
request.getRequestDispatcher("/fail.jsp").forward(request, response);
return;
} else {
//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setContentType("multipart/form-data");
//2.设置文件头:最后一个参数是设置下载文件名(如a.txt)
response.setHeader("Content-Disposition", "attachment;fileName="+fileName);
//读取要下载的文件,保存到文件输入流
FileInputStream in = new FileInputStream(getFile);
//创建输出流
OutputStream out = response.getOutputStream();
//创建缓冲区
byte buffer[] = new byte[512];
int len = 0;
//循环将输入流中的内容读取到缓冲区当中
while((len=in.read(buffer))>0){
//输出缓冲区的内容到浏览器,实现文件下载
out.write(buffer, 0, len);
}
//关闭文件输入流
in.close();
//关闭输出流
out.flush();
out.close();
}
}
}
3. 简单的FileProperty.java文件
package com.study.pojo;
public class FileProperty {
private String name;
private String type;
private String path;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}