总结:
1. 获取参数方式
1. 原生的HttpRequest,HttpSession
2. @RequestParam ,可以获取Get方法参数?后面的,也可以获取Post 方法参数
3. @PathVariable 获取路径中的参数,一般是请求路径中的。
4. @RequestHeader:获取请求头信息
5. @CookieValue:获取cookie中的值
6. @ModelAttribute获取请求请求体中的值,并直接完成对象属性的赋值
7. @RequestBody 获取请求体参数,多个参数用&&的形式连接。
如果是json格式,可以包装成对象。
8. HttpEntity。能够获取所有的请求内容。
2. 返回响应参数
1. Model ,ModelMap, Map 返回参数
2. ModelAndView 返回参数
3. @ResponseBody,返回json数据
4. ResponseEntity 自定义响应
3. 文件上传下载
3.1 文件上传
@RequestMapping("/download")
public ResponseEntity<byte[]> download(HttpServletRequest request) throws IOException {
ServletContext servletContext = request.getServletContext();
String realPath = servletContext.getRealPath("/images/test.xls");
System.out.println(realPath);
FileInputStream fileInputStream = new FileInputStream(realPath);
byte[] bytes = new byte[fileInputStream.available()];
fileInputStream.read(bytes);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("Content-Disposition","attachment;filename=test.xls");
ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(bytes,httpHeaders, HttpStatus.OK);
return responseEntity;
}
3.2 文件下载
1. Spring MVC 为文件上传提供了直接的支持,这种支持是通过即插即用的 MultipartResolver 实现的。Spring 用 Jakarta Commons FileUpload 技术实现了一个 MultipartResolver 实现类:CommonsMultipartResovler
2. Spring MVC 上下文中默认没有装配 MultipartResovler,因此默认情况下不能处理文件的上传工作,如果想使用 Spring 的文件上传功能,需现在上下文中配置 MultipartResolver。
需要jar包
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
xml配置CommonsMultipartResovler, 配置的id 必须是 multipartResolver
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<property name="maxUploadSize" value="102400"></property>
</bean>
前段和后端,
@RequestMapping("/upload")
public String upLoad(@RequestParam("desc")String desc, @RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) throws IOException {
System.out.println("---------------------"+this.getClass().getName());
String originalFilename = multipartFile.getOriginalFilename();
System.out.println(originalFilename);
multipartFile.transferTo(new File("D:\\tzwfile\\"+multipartFile.getOriginalFilename()));
return "hello";
}
上传时,必须配置 enctype = "multipart/form-data"
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>文件上传</title>
</head>
<body>
<form action="/springmvcone_war_exploded/upload" method="post" enctype="multipart/form-data">
文件名称:<input type="text" name="desc"/><br>
文件上传:<input type="file" name="file"/><br>
<input type="submit" value="submit"/><br>
</form>
</body>
</html>
上送多个文件,用数据接收,MultipartFile[]
<form action="/springmvcone_war_exploded/uploadList" method="post" enctype="multipart/form-data">
文件名称:<input type="text" name="desc"/><br>
文件上传1:<input type="file" name="file"/><br>
文件上传2:<input type="file" name="file"/><br>
文件上传3:<input type="file" name="file"/><br>
<input type="submit" value="submit"/><br>
</form>
@RequestMapping("/uploadList")
public String upLoadList(@RequestParam("desc")String desc, @RequestParam("file") MultipartFile[] multipartFiles, HttpServletRequest request) throws IOException {
System.out.println("---------------------"+this.getClass().getName());
for (MultipartFile multipartFile:multipartFiles) {
String originalFilename = multipartFile.getOriginalFilename();
System.out.println(originalFilename);
multipartFile.transferTo(new File("D:\\tzwfile\\"+multipartFile.getOriginalFilename()));
}
return "hello";
}
5. 流的 复习总结
InputStream
1. int read() ,从输入流中读取一个字节并返回。返回的是一个字节,返回的是真正读取的内容。
2. int read(byte[] b) ,从输入流中读取b.length长度的字节,并将其读取到byte数组中,真正的内容存放在byte数组中,。返回的是读取的长度。
3. int read(byte[] b ,int off ,int len) , 从输入流中读取从off开始到len长度的字节,并将其读取到byte数组中,真正的内容存放在byte数组中,。返回的是读取的长度。是2的更精确的方式。
OutputStream
1. void write(byte[] b) , 从字节数组中读取长度为b.length的字节到输出流中。
2. void write(byte[] b, int off, int len) , 从字节数组中读取从off开始到长度为b.length的字节到输出流中。
3. void write(int b) , 将指定的字节b读入输出流中