SpringBoot上传文件07

编写html文件

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>你好</title>
</head>
<body>
	<form action="fileUploadController" method = "post" enctype="multipart/form-data">
		上传文件:<input type="file" name="filename"/><br/>
		<input type="submit"/>
	</form>
</body>
</html>

 编写controller

@RestController // 表示所有方法的返回值都会自动做json格式的转换
public class fileUploadController {
	/**
	 * 处理文件上传
	 */
	@RequestMapping("/fileUploadController") // 绑定url,也就是HTML中action中所填的值
	public Map<String, Object> fileUpload(MultipartFile filename) {
											//MultipartFile的值为file对应的name 的值
		System.out.println(filename.getOriginalFilename());
		try {
			filename.transferTo(new File("e:/" + filename.getOriginalFilename()));
		} catch (IllegalStateException | IOException e) {
			e.printStackTrace();
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("msg", "ok");
		return map;
	}
}

  

编写启动器

@SpringBootApplication
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

 

设置上传文件的容量大小

在src/main/resourses下添加一个application.properties文件并添加以下内容

spring.http.multipart.maxFileSize = 200MB  上传单个文件的大小
spring.http.multipart.maxRequestSize = 200MB  上传文件的总容量

  

 

遇到了问题1:

  This application has no explicit mapping for /error, so you are seeing this as a fallback.

  Tue May 07 21:05:44 CST 2019
  There was an unexpected error (type=Internal Server Error, status=500).
  No message available
原因1:

Application启动类的位置不对.要将Application类放在最外侧,即包含所有子包

  • spring-boot会自动加载启动类所在包下及其子包下的所有组件

原因2:

在springboot的配置文件:application.yml或application.properties中关于视图解析器的配置问题:

  • 当pom文件下的spring-boot-starter-paren版本高时使用:spring.mvc.view.prefix/spring.mvc.view.suffix
  • 当pom文件下的spring-boot-starter-paren版本低时使用::spring.view.prefix/spring.view.suffix

原因3:

控制器的URL路径书写问题:

  • @RequestMapping(“xxxxxxxxxxxxxx”) ,实际访问的路径与”xxx”不符合
 真正的原因是因为HTML中的multipart写成了mutipart
posted @ 2019-05-07 21:56  朱健栋  阅读(218)  评论(0编辑  收藏  举报