标签:上传下载 tty class tac 路径 BMI start NPU tar
文件上传
1.maven的配置文件中添加一下jar
<!-- thymeleaf模板插件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2配置application.properties中的页面前后缀
spring.mvc.view.prefix=classpath:/(前端页面路径)
spring.mvc.view.suffix=.html(前端页面后缀名)
3.编写前端页面
file.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<meta charset="UTF-8" />
<title>文件上传</title>
</head>
<body>
<form action="(controller接受路径)" method="post" enctype="multipart/form-data">
<p>选择文件: <input type="file" name="fileName"/></p>
<p><input type="submit" value="提交"/></p>
</form>
</body>
</html>
4.编写controller接受方法和返回页面方法
(1)访问页面controller方法
@RequestMapping("/gouploadpage")
public String gouploadpage(){
return "file"
}
(2)上传action调用方法
@RequestMapping("/uploadfile")
@ResponseBody
public String uploadfile(@RequestParam("fileName") MultipartFile file){
if(file.isEmpty){ //判断是否有文件
return "false";
}
File dest = new File("要存放的路径");//保存文件
try{
file.transferTo(dest);
}catch(Exception e){
}
}
文件下载
1.下载的controller方法
@RequestMapping("downloadfile")
public String downLoad(HttpServletResponse response){
String filename="文件名";
File file = new File("要下载的文件路径");
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) {
e.printStackTrace();
}
System.out.println("----------file download" + filename);
try {
bis.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
2.直接访问该接口即可下载文件
直接将地址黏贴到浏览器中即可下载文件
标签:上传下载 tty class tac 路径 BMI start NPU tar
原文地址:https://www.cnblogs.com/xiaoqiang5120/p/10072659.html