请求转发
特点:
请求转发是在服务器内部进行的,客户端不会察觉到。
转发是直接将请求传递给另一个组件,而不需要浏览器的参与。
转发是通过使用 RequestDispatcher 接口的 forward() 方法来实现的。
使用场景:
当两个组件共享相同的请求信息时,可以使用请求转发。
适用于在同一 web 应用程序内部的不同组件之间进行通信,如 Servlet 到 JSP 页面的转发。
请求重定向:
特点:
请求重定向是通过发送一个 HTTP 重定向响应给客户端来实现的。
客户端收到响应后,会发起一个新的请求到指定的 URL。
重定向导致客户端和服务器之间产生两次请求-响应往返。
使用场景:
当两个组件不共享相同的请求信息,或者要将请求发送到不同的 web 应用程序时,可以使用请求重定向。
适用于在不同的 web 应用程序之间导航,或者在同一应用程序内部的不同上下文之间导航。
请求转发:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Controller
@RequestMapping("/user")
public class UserController() {
@GetMapping("/getUser")
public void getUser(HttpServletRequest request, HttpServletResponse response) {
request.getRequestDispatcher("/user/addUser").forward(request,response);
}
@PostMapping("/addUser")
public String addUser() {
// 返回页面
return "user";
}
}
请求重定向:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Controller
@RequestMapping("/user")
public class UserController() {
@GetMapping("/getUser")
public void getUser(HttpServletRequest request, HttpServletResponse response) {
response.sendRedirect("/user/addUser");
}
@PostMapping("/addUser")
public String addUser() {
// 返回页面
return "user";
}
}
请求转发
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Controller
@RequestMapping("/user")
public class UserController() {
@GetMapping("/getUser")
public String getUser(HttpServletRequest request, HttpServletResponse response) {
return "forward:/user/addUser";
}
@PostMapping("/addUser")
public String addUser() {
// 返回页面
return "user";
}
}
请求重定向
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Controller
@RequestMapping("/user")
public class UserController() {
@GetMapping("/getUser")
public String getUser(HttpServletRequest request, HttpServletResponse response) {
return "redirect:/user/addUser";
}
@PostMapping("/addUser")
public String addUser() {
// 返回页面
return "user";
}
}
<properties>
<spring.version>5.3.6spring.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.thymeleafgroupId>
<artifactId>thymeleaf-spring5artifactId>
<version>3.0.12.RELEASEversion>
dependency>
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.4version>
dependency>
dependencies>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.atguigu.controller"/>
<mvc:view-controller path="/" view-name="index">mvc:view-controller>
<mvc:annotation-driven>mvc:annotation-driven>
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="order" value="1"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine">
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/"/>
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="characterEncoding" value="UTF-8" />
bean>
property>
bean>
property>
bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1048576">property>
bean>
beans>
package com.atguigu.controller;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
@Controller
@RequestMapping("/file")
public class FileController {
/**
* 文件下载
* @param file
* @return
* @throws IOException
*/
@GetMapping("/getFile")
public ResponseEntity<byte[]> downFile(MultipartFile file) throws IOException {
// 1.获取文件的路径
String filePath = "E:/file/mybatis思维导图.png";
// 2.获取一个输入流
InputStream in = new FileInputStream(filePath);
// 2. 返回响应
byte[] bytes = new byte[in.available()];
in.read(bytes);
HttpHeaders headers = new HttpHeaders();
// 讲UTF-8 转化为 ISO-8859-1
String fileName = new String("mybatis思维导图.png".getBytes("UTF-8"),"ISO-8859-1");
headers.add("Content-Disposition","attachment;filename="+fileName);
return new ResponseEntity<>(bytes,headers, HttpStatus.OK);
}
/**
* 文件上传
*
* @return
*/
@PostMapping("/uploadFile")
public String uploadFile(MultipartFile file) throws IOException {
// 文件夹前半段
String dirPath = "E:/file";
SimpleDateFormat dateFormat = new SimpleDateFormat("/yyyy/MM/DD/");
dirPath += dateFormat.format(new Date());
File fileDirPath = new File(dirPath);
// 如果文件夹不存在则创建该文件夹
if (!fileDirPath.exists()){
fileDirPath.mkdirs();
}
// 文件的全名 从前端传过来的
String fileFullName = file.getOriginalFilename();
// 文件后缀名
String fileSuffixName = fileFullName.substring(fileFullName.lastIndexOf("."));
// 文件的保存名字
String fileName = UUID.randomUUID().toString().replace("-","")+fileSuffixName;
file.transferTo(new File(dirPath+fileName));
return "success_file";
}
}
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>文件的上传和下载title>
<base th:href="@{/}">
head>
<body>
<a href="file/getFile">下载文件a>
<br>
<form method="post" action="file/uploadFile" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit">上传button>
form>
body>
html>
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>文件的上传和下载title>
<base th:href="@{/}">
head>
<body>
<p>文件上传成功p>
<br>
body>
html>