文件上传和下载
一.文件上传
1.新建html文件
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" >
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="./css/layui.css">
</head>
<body>
<script src="./js/layui.js"></script>
<p th:text="${msg}" style="color: #0000FF; "></p>
<div>
<form th:action="@{/fileUpload}" enctype="multipart/form-data" method="post">
<input type="file" name="file" />
<input type="submit" value="上传文件" />
</form>
</div>
<!--<p th:text="${msg}" style="color: #0000FF; margin-bottom: 10px;margin-left: 10px;"></p>-->
<a class="layui-btn layui-btn-sm layui-btn-primary" style="margin-bottom: 10px;margin-left: 10px;" th:href="@{/download}">下载文件</a>
</body>
</html>
2.controller处理文件上传请求
// 文件上传请求处理
@PostMapping("/fileUpload")
public String upload(MultipartFile file, Model model) throws IOException {
// 采用file.Transto来保存上传的文件
if (file.isEmpty()){
model.addAttribute("msg","请选择文件");
return "file";
}
// fileName: 文件名
String fileName=file.getOriginalFilename();
// filepath:文件保存的路径
String filePath="C:\\Users\\韩英俊\\Desktop\\SpringBootStudy\\src\\main\\resources\\static\\uploadFiles\\uploadFile";
File dest=new File(filePath+"/"+fileName);
try {
file.transferTo(dest);
model.addAttribute("msg","上传成功");
return "file";
} catch (Exception e) {
e.printStackTrace();
}
model.addAttribute("msg","文件上传失败");
return "email";
}
注意:filePath是设置保存上传文件的路径,需要自己去更改设定。
3.上传文件验证
文件已经上传到指定的位置,可以打开查看。
二.文件下载
1、首先,准备一个要下载的文件,我这里放了一个han.docx的word文档。
2、写controller处理文件下载请求
@RequestMapping("/download")
public String downLoad(HttpServletResponse response,Model model) throws UnsupportedEncodingException {
String filename="han.docx";
String filePath = "C:\\Users\\韩英俊\\Desktop\\SpringBootStudy\\src\\main\\resources\\static\\uploadFiles\\uploadFile" ;
File file = new File(filePath + "/" + filename);
if(file.exists()){ //判断文件父目录是否存在
response.reset();
// 设置response的Header请求头
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("UTF-8"), "ISO-8859-1"));
response.addHeader("Content-Length", "" +file.length());
// response.setContentType("application/vnd.ms-excel;charset=UTF-8");
// response.setCharacterEncoding("UTF-8");
// // response.setContentType("application/force-download");
// response.setHeader("Content-Disposition", "attachment;fileName=" + java.net.URLEncoder.encode(filename,"UTF-8"));
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();
model.addAttribute("msg","文件下载成功");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
注意:这里的filename和filePath是你要下载文件的文件名和存放路径,根据自己的情况更改。
3.下载验证
每点击一次下载就会对文件进行一次下载请求。
打开这个下载的文件han(10).docx查看,就是准备的文件。