springboot整合thymeleaf模板,上传⽂件及时回显和下载的功能。
1、第⼀步在配置⽂件中⾃定义⽂件上传路径,访问路径
第⼆步需要创建两个html页⾯,模板选⽤thymeleaf
<!--⽂件上传第⼀次跳转-->
<!DOCTYPE html>
<html lang="en" xmlns:th="">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:action="@{/upload}" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="上传">
</form>
</body>
</html>
//回显的时候取值可能会出现红⾊下划线这是正常情况不⽤理会
<!--⽂件上传以后回显并且下载-->
<!DOCTYPE html>
<html lang="en" xmlns:th="">
<head>
<meta charset="UTF-8">
<title>上传⽂件</title>
</head>
<body>
<img th:src="@{${filePath}}" alt="">
<a th:href="@{/download/(filepath=${filePath},fileName=${fileName})}">下载</a>
</body>
</html>
第三步就是建⼀个控制层Controller类并且同时创建配置类Configration
2、下⾯直接上代码:
1) 控制层代码:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
@Controller
public class FileController {
private static final Logger LOGGER =
@Value("${uploadpath}")
private String uploadpath;
@GetMapping("/toUpload")
public String toUpload(){
return"index/upload";
}
@Value("${vpath}")
public String vpath;
@PostMapping("/upload")
public String upload(MultipartFile file, HttpServletRequest request, ModelMap map){ //获取上传的⽂件名
String OriginalFilename();
<("⽂件名:"+filename);
//构建保存的位置,绝对路径+⽂件名
File filel=new File(uploadpath,filename);
//如果⽗⽬录不存在,则创建⽗⽬录
if(!ParentFile().exists()){
}
try{
}catch(IOException e){
//记录错误⽇志
<("上传⽂件异常",e);
}
map.put("fileName",filename);
map.put("filePath",vpath+filename);// uploadFile/a.jpg
return"index/fileInfo";
}
//⽂件下载
@RequestMapping("/download")
public ResponseEntity<byte[]>download(
String fileName
)throws IOException {
/*    //获取⽂件真实路径
String realPath = ServletContext().getRealPath(path);*/ //创建⽂件对象
File file=new File(uploadpath,fileName);
//构建响应头
HttpHeaders headers=new HttpHeaders();
//设置以附件形式打开
headers.setContentDispositionFormData("attachment",
new Bytes("UTF-8"),
"iso-8859-1"));
//设置⽂档类型为stream
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(
thymeheaders,//头部信息
HttpStatus.CREATED);//http状态码
}
}
}
2)配置类代码:
import org.springframework.beans.factory.annotation.Value;
import t.annotation.Configuration;
import org.springframework.fig.annotation.ResourceHandlerRegistry; import org.springframework.fig.annotation.WebMvcConfigurer;
/*
* ⽂件上传路径映射配置类
* WebMvcConfigurer ⽤来配置springmvc中的⼀些配置信息
* */
@Configuration//配置类注解
public class WebAppConfig implements WebMvcConfigurer {
@Value("${uploadpath}")
private String uploadpath;
@Value("${vpath}")
private String vpath;
//添加资源映射
/*
* ResourceHandlers 相当于springmvc配置resource配置项
* */
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){
registry.addResourceHandler(vpath +"**")//指定请求URL
//"file:D:/upload"
.addResourceLocations("file:"+ uploadpath);//指定映射到的资源
}
}
效果图:
在配置⽂件中还需要配置上传的⼤⼩(根据⾃⼰的需求配置)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。