首页 > 其他分享 >Spring MVC入门(八):文件上传下载

Spring MVC入门(八):文件上传下载

时间:2022-10-01 13:39:46浏览次数:53  
标签:文件 String Spring 上传下载 fileName MVC photo new 上传

文件上传下载

  • 导入文件上传所需的依赖
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
  • springmvc配置文件
# 配置跳转到上传下载的页面
<mvc:view-controller path="/file" view-name="file"></mvc:view-controller>
# http://localhost:8080/上下文路径/ file

<!--配置文件上传解析器,将上传的文件封装为MultipartFile-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
  • 编写thymeleaf页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>测试文件上传和下载</title>
</head>
<body>
<a th:href="@{/testDown}">下载1.jpg</a><br>
<form th:action="@{/testUp}" method="post" enctype="multipart/form-data">
头像:<input type="file" name="photo"><br>
<input type="submit" value="上传">
</form>
</body>
</html>
  • 编写控制器
@Controller
public class FileUpAndDownController {

@RequestMapping("/testDown")
public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws IOException {
//获取ServletContext对象
ServletContext servletContext = session.getServletContext();
//获取服务器中文件的真实路径
String realPath = servletContext.getRealPath("/static/img/1.jpg");
System.out.println(realPath);
//创建输入流
InputStream is = new FileInputStream(realPath);
//创建字节数组
byte[] bytes = new byte[is.available()];
//将流读到字节数组中
is.read(bytes);
//创建HttpHeaders对象设置响应头信息
MultiValueMap<String, String> headers = new HttpHeaders();
//设置要下载方式以及下载文件的名字
headers.add("Content-Disposition", "attachment;filename=1.jpg");
//设置响应状态码
HttpStatus statusCode = HttpStatus.OK;
//创建ResponseEntity对象
ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(bytes, headers, statusCode);
//关闭输入流
is.close();
return responseEntity;
}

@RequestMapping("/testUp")
public String testUp(MultipartFile photo, HttpSession session) throws IOException {
//获取上传的文件的文件名
String fileName = photo.getOriginalFilename();
//获取上传的文件的后缀名
String suffixName = fileName.substring(fileName.lastIndexOf("."));
//将UUID作为文件名
String uuid = UUID.randomUUID().toString().replaceAll("-","");
//将uuid和后缀名拼接后的结果作为最终的文件名
fileName = uuid + suffixName;
//通过ServletContext获取服务器中photo目录的路径
ServletContext servletContext = session.getServletContext();
String photoPath = servletContext.getRealPath("photo");
File file = new File(photoPath);
//判断photoPath所对应路径是否存在
if(!file.exists()){
//若不存在,则创建目录
file.mkdir();
}
String finalPath = photoPath + File.separator + fileName;
//上传文件
photo.transferTo(new File(finalPath));
return "success";
}
}

Spring MVC入门(八):文件上传下载_html



标签:文件,String,Spring,上传下载,fileName,MVC,photo,new,上传
From: https://blog.51cto.com/chniny/5728220

相关文章

  • Spring MVC入门(七):HttpMessageConverter
    简介HttpMessageConverter,报文信息转换器,将请求报文转换为Java对象,或将Java对象转换为响应报文HttpMessageConverter提供了两个注解和两个类型:@RequestBody,@ResponseBody,Req......
  • Spring MVC入门(五):视图
    视图简介SpringMVC中的视图是View接口,视图的作用渲染数据,将模型Model中的数据展示给用户SpringMVC视图的种类很多,默认有转发视图和重定向视图当工程引入jstl的依赖,转发视图......
  • SpringBoot+Vue疫苗接种管理系统 新冠疫苗接种系统 预约疫苗接种管理系统Java
    ......
  • Spring MVC入门(四):域对象共享数据
    使用ServletAPI向request域对象共享数据#后端:向request域对象中添加数据,并转发到success页面@RequestMapping("/testServletAPI")publicStringtestServletAPI(HttpServle......
  • Spring MVC基础(一):HelloWorld案例
    构建1个maven项目pom.xml<packaging>war</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifa......
  • Spring MVC入门(一):入门案例
    mvc简介MVC是一种软件架构的思想,将软件按照模型、视图、控制器来划分M:Model,模型层,指工程中的JavaBean,作用是处理数据JavaBean分为两类:一类称为实体类Bean:专门存储业务数......
  • gradle构建spring boot项目
    当前​​案例地址​​构建gradle项目配置字符集、注解生效激活、JavaCompiler使用IDE自带的gradle测试打包​​参考​​拉取依赖dependencies{testCompilegroup:'ju......
  • spring boot导出word
    案例一​​参考​​使用这种方式时会报错:​​org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException:Novalidentr​​案例二导出excel导出word导出word表格导......
  • springboot导入(导出)excel
    案例一导入sql​​启动项目访问localhost:8080进入首页​​​​可将数据库中数据导出到本地​​​​将导出本地的excel修改后再次上传,查看数据库​​......
  • 文件上传下载
    案例一demo为​​chnx/springboot/file-demo​​​​项目地址​​​​参考​​启动项目访问文件上传下载页面选择文件并上传后台打印出文件上传后的目录复制目录并查看文件......