downloadOne(HttpServletRequest req,HttpServletResponse response) throws IOException{
String fileName = req.getParameter("fileName");// 设置文件名,根据业务需要替换成要下载的文件名
// String layerId = req.getParameter("layerId");
String downloadDir = req.getSession().getServletContext().getRealPath("/") +"upload/";
// String savePath = req.getSession().getServletContext().getRealPath("/") +"download/";
downloadDir=downloadDir.substring(0,downloadDir.length()-1);
downloadDir=downloadDir+"\\";//下载目录
String realPath=downloadDir+fileName;//
File file = new File(realPath);//下载目录加文件名拼接成realpath
if(file.exists()){ //判断文件父目录是否存在
// response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
byte[] buffer = new byte[1024];
FileInputStream fis = null; //文件输入流
BufferedInputStream bis = null;
OutputStream os = null; //输出流
try {
os = response.getOutputStream();
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
int i = bis.read(buffer);
while(i != -1){
os.write(buffer);
i = bis.read(buffer);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("----------file download" + fileName);
try {
bis.close();
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return api.returnJson(2,"fail"+realPath+fileName);
}
```
主要就是调IO流,然后下载罢了,拼一下路径和文件名即可
**3.效果一览**

觉得有用就给颗小????吧~
**项目仅供测试学习使用,拒绝任何形式的商业用途,转侵删。
项目源码关注公众号Code In Java,回复"SpringBoot上传下载"即可获取。除此之外,还有Java学习图谱,数据结构与算法资料等各种资料都可以在后台获取。**
关注公众号:Code In Java
资源,项目,面试题一网打尽
希望与你成为Java技术的同路人SpringBoot实现上传下载(二)
原文:https://www.cnblogs.com/Ricardo-L-Song/p/11636849.html