获取 HttpServletRequest&&HttpServletResponse对象
package com.painter.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
/**
* @Author: Painter
* @project_name: spring_mvc
* @system_login: sunshine
* @time: 2022/10/110:00
*/
@RestController
@RequestMapping("/httpservlet")
public class MyControllerA { // 接收json数据
// @Autowired
// private HttpServletRequest request;
// @Autowired
// private HttpServletResponse response;
/**
* http://localhost:8080/httpservlet/httpservlet1
* @param userName
* @param password
* @param request HttpServletRequest 在访问此url映射时,会自动获取
* @param response HttpServletResponse
* @return
* @throws Exception
*/
@RequestMapping(value = "/httpservlet1")
public String httpservlet1(String userName, String password , HttpServletRequest request, HttpServletResponse response) throws Exception {
String userName1 = request.getParameter("userName"); // 获取访问url传递的指定参数
String password1 = request.getParameter("password"); // 获取访问url传递的指定参数
if ("painter".equals(userName1)&&"123456".equals(password1)) {
// 重定向(跳转到指定的url)
response.sendRedirect("http://localhost:8080/httpservlet/image");
}
return "";
}
/**
* http://127.0.0.1:8080//httpservlet/httpservlet2?userName=painter&password=123456
* @param userName 访问url映射时传递的参数(必须和传递的参数名相同)
* @param password
* @return
* @throws Exception
*/
@RequestMapping(value = "/httpservlet2")
public String httpservlet2(String userName, String password) throws Exception {
// 获取HttpServletRequest
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
// 获取HttpServletRequest
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
String userName1 = request.getParameter("userName"); // 获取访问url传递的指定参数
String password1 = request.getParameter("password"); // 获取访问url传递的指定参数
if ("painter".equals(userName1)&&"123456".equals(password1)) {
// 重定向(自动跳转到指定的url)
response.sendRedirect("http://localhost:8080/httpservlet/image");
}
return "";
}
/**
* 访问此url映射时,响应图片
* http://localhost:8080/httpservlet/image
* @param request
* @param response
* @return
* @throws Exception
*/
@RequestMapping(value = "/image")
public String image(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType("image/jpg"); // 设置响应内容类型
File file = new File("src/main/webapp/00UWqm.jpg"); // 创建以个file对象(打开一个文件对象)
FileInputStream fileInputStream = new FileInputStream(file); // 获取文件对象的文件输入流
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); // 获取文件对象的缓冲区输入流
ServletOutputStream outputStream = response.getOutputStream(); // 获取访问url时的输出流
byte[] bytes = new byte[1024]; // 创建一个字节数组
int l; // 创建一个整数变量 记录读取的长度
while ((l = bufferedInputStream.read(bytes)) != -1) { // 循环读取缓冲区的字节信息
outputStream.write(bytes, 0, l); // 将字节数组的字节信息写入访问url时的输出流
}
return "OK";
}
}