Spring Boot - Web 应用开发 - 文件上传
2018-03-17 01:05
597 查看
Spring Boot 默认使用 Spring MVC 包装好的解析器进行上传
添加代码
配置(已被弃用)
添加代码
<form method="post" enctype="multipart/form-data" action="/file/upload"> 文件:<input type="file" name="file"> <input type="button" value="上传"> </form>
@Controller @RequestMapping("/file") public class FileController { private static Logger logger = LoggerFactory.getLogger(FileController.class); @RequestMapping("/upload") public String upload(@RequestParam("file") MultipartFile file) { if (file.isEmpty()) { return "文件为空"; } // 获取文件名 String fileName = file.getName(); logger.info("上传文件名为:" + fileName); // 获取文件的后缀名 String suffixName = fileName.substring(fileName.lastIndexOf(".")); logger.info("上传的后缀名为:" + suffixName); // 文件上传路径 String filePath = "D:/tempFile"; // 解决中文问题,Linux 下中文路径,图片显示问题 // fileName = UUID.randomUUID() + suffixName; File dest = new File(filePath + fileName); // 检查是否存在目录 if (!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); } try { file.transferTo(dest); return "上传成功"; } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return "上传失败"; } }
配置(已被弃用)
spring.http.multipart.enabled=true spring.http.multipart.file-size-threshold=0 spring.http.multipart.location= spring.http.multipart.max-file-size=1MB spring.http.multipart.max-request-size=10Mb
相关文章推荐
- SpringBoot | 第十七章:web应用开发之文件上传
- Spring Boot基础教程12-web应用开发-文件上传
- SpringBoot | 第十七章:web应用开发之文件上传
- springBoot(10):web开发-文件上传
- 基于spring-boot的web应用,ckeditor上传文件图片文件
- Spring Boot基础教程8-web应用开发-模板引擎jsp
- Spring Boot: 开发web 应用 - 03 Spring Framework 回顾
- Spring boot(16) spring boot 线上故障 上传文件出错:org.springframework.web.multipart.MultipartException: Could
- 【Spring Boot】web应用开发-Servlets, Filters, listeners
- Spring Boot: 开发web 应用 - 01 创建项目
- webuploader在springMVC+jquery+Java开发环境下的大文件分片上传
- Idea中使用Springboot和webuploader插件进行文件上传
- Spring3文件上传,提速你的Web开发
- Spring Boot: 开发web 应用 - 04 (Optional) Web MVC - Look Under The Hood
- Spring3文件上传,提速你的Web开发
- Spring Boot干货系列:(五)开发Web应用之JSP篇
- Android应用开发中webview上传文件的几种思路
- Spring Boot基础教程9-web应用开发-错误处理
- Spring Boot在开发时实现热部署(开发时修改文件保存后自动重启应用)(spring-boot-devtools)
- 用Spring Boot & Angular2快速开发文件上传服务