1、@ResponseBody注解
该注解会去找HttpMessageConverter<T>,将需要返回的数据转变成HttpOutputMessage,返回给浏览器
该注解作用一般可以将数据封装成json格式的数据返回回去
Maven
<!-- Json Begin -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<!-- Json End -->
其他功能
1、如果不需要某个字段被打包的话
使用@JsonIgnore注解get方法上
前提必须有这个依赖(上面已经有了)
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
2、给json数据(封装时候)起一个别名
2、@RequestBody注解
将请求的数据转化成String
controller
@RequestMapping(value = "/filetest",method = RequestMethod.POST)
public String filetest(@RequestBody String string){
try {
String s2 = new String(string.getBytes("iso-8859-1"), "utf8");
System.out.println(s2);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "login";
}
html
<form action="/filetest" method="post" enctype="multipart/form-data">
<input type="file" name="string">
<input type="submit" value="提交">
</form>
文件上传
导入jar包
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
Controller
@RequestMapping(value = "/fileupload",method = RequestMethod.POST)
public String fileupload(MultipartFile file,HttpServletRequest httpServletRequest) throws IOException {
//获取文件的名字
String originalFilename = file.getOriginalFilename();
InputStream inputStream = file.getInputStream();
String path = httpServletRequest.getSession().getServletContext().getRealPath("/")+originalFilename;
FileOutputStream fileOutputStream = new FileOutputStream(path,true);
int len=-1;
byte[] bytes = new byte[1024];
while ((inputStream.read(bytes))!=-1){
fileOutputStream.write(bytes);
}
fileOutputStream.close();
inputStream.close();
return "login";
}
3、ResponseEntity
文件下载
@RequestMapping(value = "/filedownload",method = RequestMethod.GET)
public ResponseEntity filedownload(HttpServletRequest httpServletRequest) throws IOException {
//设置响应体
byte[] body = null;
ServletContext servletContext = httpServletRequest.getSession().getServletContext();
InputStream resourceAsStream = servletContext.getResourceAsStream("/WEB-INF/abc.txt");
body = new byte[resourceAsStream.available()];
resourceAsStream.read(body);
resourceAsStream.close();
// 方式二,设置响应体
// System.out.println(httpServletRequest.getSession().getServletContext().getRealPath("/"));
// String filepath = httpServletRequest.getSession().getServletContext().getRealPath("/")+"WEB-INF\\abc.txt";
// FileInputStream fileInputStream = new FileInputStream(filepath);
// body = new byte[fileInputStream.available()];
// fileInputStream.read(body);
// fileInputStream.close();
//设置响应头
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Content-Disposition","attachment;filename=abc.txt");
//设置响应码
HttpStatus httpStatus = HttpStatus.OK;
ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(body,httpHeaders,httpStatus);
return responseEntity;
}