HTTPMessageConverter
报文信息转换器
我的代码
复习:
post请求才有请求体
json:大括号是对象 方括号是数组
json里map转换为json对象,list转换为json数组
json两种存在形式:json对象指的是JavaScript,json字符串是浏览器接受的,或者说java接收的
-
@RequestBody可以获取请求体,需要在控制器方法设置一个形参,使用@RequestBody进行标识,当前请求的请求体就会为当前注解所标识的形参赋值
-
RequestEntity封装请求报文的一种类型,需要在控制器方法的形参中设置该类型的形参,当前请求的请求报文就会赋值给该形参,可以通过getHeaders()获取请求头信息,通过getBody()获取请求体信息
-
@ResponseBody用于标识一个控制器方法,可以将该方法的返回值直接作为响应报文的响应体响应到浏览器
-
ResponseEntity用于控制器方法的返回值类型,该控制器方法的返回值就是响应到浏览器的响应报文
@RestController注解是springMVC提供的一个复合注解,标识在控制器的类上,就相当于为类添加了@Controller注解,并且为其中的每个方法添加了@ResponseBody注解
@Controller
public class HttpController {
@RequestMapping("/testRequestBody")
public String testRequestBody(@RequestBody String requestBody){
System.out.println("requestBody"+requestBody);
return "success";
}
@RequestMapping("/testRequestEntity")
public String testRequestBody( RequestEntity<String> entity){
//RequestEntity是整个请求报文的信息
System.out.println(entity.getHeaders());
System.out.println(entity.getBody());
return "success";
}
@RequestMapping("/testResponseBody")
@ResponseBody
public String testResponseBody(){
return "success,ResponseBody";
}
@RequestMapping("/testResponseUser")
@ResponseBody
public User testResponseUser(){
//User没有写toString
//会直接报错,所以要用json
return new User(1001,"admin","123465",18,"男");
//然后 加入 jackson——databind依赖 直接可用!!放回JSon
}
}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.1</version>
</dependency>
使用ResponseEntity实现文件下载 和上传
@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/test.png");
//创建输入流
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=test.png");
//设置响应状态码
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 {
//System.out.println(photo.getName());//表单name属性
//System.out.println(photo.getOriginalFilename());//上传的文件名
String filename = photo.getOriginalFilename();
ServletContext servletContext = session.getServletContext();
String realPath = servletContext.getRealPath("/photo");
//动态创建文件夹
File file = new File(realPath);
//判断文件是否存在
if (!file.exists()) {
file.mkdir();
}
//上传最终路径 File.separator就是分隔符 / or \ 根据系统来判断分隔符
String finalFile = realPath+File.separator+filename;
photo.transferTo(new File(finalFile));
return "success";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>文件上传和下载</title>
</head>
<body>
<a th:href="@{/testDown}">testDown</a>
<!--分成多段以流的形式传输:multipart/form-data-->
<form th:action="@{/testUp}" method="post" enctype="multipart/form-data">
图片<input type="file" name="photo">
<input type="submit" value="上传">
</form>
</body>
</html>
文件上传需要的:
依赖:
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
配置:
<!--必须通过文件解析器的解析才能将文件转换为MultipartFile对象-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
防止上传文件后被覆盖的方法(上面的方法会覆盖):
@RequestMapping("/testUp")
public String testUp(MultipartFile photo,HttpSession session) throws IOException {
//System.out.println(photo.getName());//表单name属性
//System.out.println(photo.getOriginalFilename());//上传的文件名
String fileName = photo.getOriginalFilename();
//防止覆盖
//从最后一个点开始
//(包括点)substring包前不包后eg: .jpg
String suffixName = fileName.substring(fileName.lastIndexOf("."));
//UUID作为文件名一部分
String uuid = UUID.randomUUID().toString();
//拼接 替换原来的文件名
fileName =uuid+suffixName;
ServletContext servletContext = session.getServletContext();
String realPath = servletContext.getRealPath("/photo");
//动态创建文件夹
File file = new File(realPath);
//判断文件是否存在
if (!file.exists()) {
file.mkdir();
}
//上传最终路径 File.separator就是分隔符 / or \ 根据系统来判断分隔符
String finalFile = realPath+File.separator+fileName;
photo.transferTo(new File(finalFile));
return "success";
}