Human0722's blog Human0722's blog
首页
  • Spring

    • Spring Framework
    • Spring Boot
    • Spring Cloud
  • CCNA
  • Vue

    • Vue2
日本语
导航
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Human0722

Gravity always win
首页
  • Spring

    • Spring Framework
    • Spring Boot
    • Spring Cloud
  • CCNA
  • Vue

    • Vue2
日本语
导航
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • SpringMVC 文件下载与上传

Xueliang
2021-03-19
Java
目录

SpringMVC 文件下载与上传

SpringMVC 上传&&下载的代码

# 1. 文件下载

// @Controller
@RequestMapping("/download", method = RequestMethod.GET)
public MultipartEntry<byte[]> download(HttpSession session) throws IOException{
    // Get file store location on server
    String realPath = session.getServletContext().getRealPath("img");
    String finalPath = realPath + File.separator + "2.jgp";
    InputStream is = new FileInputStream(finalPath);
    // available(): 获取输入流所读取文件的最大字节数
    byte[] b = new byte[is.available()];
    is.read(b);
    // 设置响应头
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Disposition", "attachment;filename=zzz.jgp");
    // 设置响应状态
    HttpStatus statusCode = HttpStatus.OK;
    ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(b, headers, statusCode);

    return entity;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 2. 文件上传

从表单开始,到 SpringMVC 的代码设置

# 2.1 html表单

<form action="/up" method="post" enctype="multipart/form-data">
    <input type="file" name="uploadFile">
    <input type="submit" name="upload">
</form>
1
2
3
4

# 2.2 控制器处理方法

// 原生方法
@RequestMapping(value = "up", method = RequestMethod.POST)
public String up(String desc, MultipartFile uploadFile, HttpSession session)  
throws IOException{
    // get uploadFile filename
    String fileName = uploadFile.getOriginalFilename();
    String path = session.getServletContext().getRealPath("photo") + File.separator + fileName;
    // get inputStream
    InputStream is = uploadFile.getInputStream();
    //get outputStream
    File file = new File(path);
    OutputStream os = new FileOutputStream(file);
    //write
    byte[] b = new byte[1024];
    while((i = is.read(b)) != -1) {
        os.write(b, 0, i);
    }
    os.close();
    is.close();
    return "success";
}

// 封装类方法
    @RequestMapping(value="/up", method=RequestMethod.POST)
	public String up(String desc, MultipartFile uploadFile, HttpSession session) throws IOException {
		//获取上传文件的名称
		String fileName = uploadFile.getOriginalFilename();
		String finalFileName = UUID.randomUUID() + fileName.substring(fileName.lastIndexOf("."));
		String path = session.getServletContext().getRealPath("photo") + File.separator + finalFileName;
		File file = new File(path);
		uploadFile.transferTo(file);
		return "success";
	}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

# 2.3 配置 multipartResolver

multipartResolver 是用来处理上传文件的,前置条件: 导入 commmons.io、 commons.fileupload 包。

在 SpringMVC 的配置文件中新增

<bean id="multipartResolver" class="xxx.xxx.xxx.CommonsMultipartResolver">
    <property name="defaultEncoding" value="UTF-8" />
    <property name="maxUploadSize" value="10240000">
</bean>
1
2
3
4

# 2.4 Tips

  • 上传文件保存位置的 photo 文件夹,要放在 WebContent 目录下,有时候空目录部署时会被忽略掉,最好在目录中放个文件。
  • 上传文件处理函数的参数, Multipart uploadFile 中的属性名要和表单中 file 类型的 input 的 name 属性值一致,或者用 @RequestParam() 的方式来绑定。
上次更新: 2022/05/06, 18:09:58
最近更新
01
DefineSprintBootStarter
03-23
02
Spring MVC 启动流程分析
03-23
03
Redis
03-23
更多文章>
Theme by Vdoing | Copyright © 2019-2024 Human0722 | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式