Spring-Boot-文件上传
发表于|更新于
|阅读量:
Spring-Boot-文件上传
<Excerpt in index | 首页摘要>
<The rest of contents | 余下全文>
编写Controller
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
| package com.caicai.springbootstaticresources1.controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.IOException;
@RestController public class FileController {
@RequestMapping(value = "/fileUploadController") public String fileUpload(@RequestParam("filename") MultipartFile file){ System.out.println(file.getName()); try { file.transferTo(new File("F:/"+file.getName())); } catch (IOException e) { e.printStackTrace(); } return "success"; } }
|
编写上传界面
为了方便在static文件夹下建立upload.html
1 2 3 4 5 6 7 8 9 10 11 12 13
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>文件上传</title> </head> <body> <form action="fileUploadController" method="post" enctype="multipart/form-data"> 上传文件:<input type="file" name="filename"> <input type="submit"> </form> </body> </html>
|
设置上传文件大小的默认值
在resources下建立application.yml文件
1 2 3 4 5 6
| spring: servlet: multipart: max-file-size: 10MB #设置单个上传文件的大小 max-request-size: 100MB #设置一次请求上传文件的总容量 enabled: true
|