UserController
package com.spring.ssm.controller;
import com.alibaba.fastjson.JSON;
import com.spring.ssm.dao.UserDao;
import com.spring.ssm.entity.UserEntity;
import com.spring.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.Map;
/***
* @ClassName: UserController
* @Description: TODO
* @author: yanyd
* @Date: 23:20 2020/8/24
* @version : V1.0
*/
@RestController
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping(value = "/listUser/{name}")
public String listUser(@PathVariable("name") String name, HttpServletRequest request, HttpServletResponse response) {
List<UserEntity> userEntities = userService.listUserByName(name);
return JSON.toJSONString(userEntities);
}
@GetMapping(value = "/listUser/v1")
public String listUserV1(@RequestParam(value = "name") String name, HttpServletRequest request, HttpServletResponse response) {
List<UserEntity> userEntities = userService.listUserByName(name);
return JSON.toJSONString(userEntities);
}
@PostMapping(value = "/commitUserInfo", produces = {"application/json;charset=utf-8"})
public String listUser(@RequestBody Map map) {
return JSON.toJSONString(map);
}
@GetMapping(value = "/download")
public void download(@RequestParam("path") String path, HttpServletResponse response) throws Exception {
path = "E:\\个人文件\\壁纸\\" + path;
File file = new File(path);
FileInputStream fileInputStream = new FileInputStream(file);
int a = fileInputStream.available();
byte[] bytes = new byte[a];
fileInputStream.read(bytes);
//设置response的Header
//Content-Disposition属性有两种类型:inline 和 attachment inline :将文件内容直接显示在页面 attachment:弹出对话框让用户下载
/***************下载**************/
//response.setHeader("Content-Type", "application/octet-stream"); // 设置response的Header
//response.addHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(file.getName(), "UTF-8"));
/***************预览**************/
response.setHeader("Content-Type", "image/jpeg"); // 设置response的Header
response.addHeader("Content-Disposition", "inline;filename=" + java.net.URLEncoder.encode(file.getName(), "UTF-8"));
response.addHeader("Content-Length", "" + bytes.length);
OutputStream outputStream = response.getOutputStream();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
bufferedOutputStream.write(bytes);
bufferedOutputStream.flush();
bufferedOutputStream.close();
outputStream.close();
}
@PostMapping(value = "/uploadFile")
public String uploadFile(@RequestParam(value = "file") MultipartFile multipartFile) throws Exception {
//获取该文件的文件名
String fileName = multipartFile.getOriginalFilename();
File transFile = new File("d:/", fileName);
if (!transFile.exists()) {
transFile.createNewFile();
}
// 保存
try {
multipartFile.transferTo(transFile);
} catch (Exception e) {
e.printStackTrace();
}
return "上传成功";
}
}
UserDao
package com.spring.ssm.dao;
import com.spring.ssm.entity.UserEntity;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/***
* @ClassName: UserDao
* @Description: TODO
* @author: yanyd
* @Date: 23:18 2020/8/24
* @version : V1.0
*/
public interface UserDao {
List<UserEntity> listUser();
List<UserEntity> listUserByName(@Param("name")String name);
}
UserEntity
package com.spring.ssm.entity;
/***
* @ClassName: UserEntity
* @Description: TODO
* @author: yanyd
* @Date: 23:19 2020/8/24
* @version : V1.0
*/
public class UserEntity {
private int id;
private String name;
private Double num;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getNum() {
return num;
}
public void setNum(Double num) {
this.num = num;
}
}
UserServiceImpl
package com.spring.ssm.service.impl;
import com.spring.ssm.dao.UserDao;
import com.spring.ssm.entity.UserEntity;
import com.spring.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/***
* @ClassName: UserServiceImpl
* @Description: TODO
* @author: yanyd
* @Date: 23:40 2020/8/24
* @version : V1.0
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List<UserEntity> listUser() {
return userDao.listUser();
}
@Override
public List<UserEntity> listUserByName(String name) {
return userDao.listUserByName(name);
}
}
UserService
package com.spring.ssm.service;
import com.spring.ssm.entity.UserEntity;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/***
* @ClassName: UserService
* @Description: TODO
* @author: yanyd
* @Date: 23:40 2020/8/24
* @version : V1.0
*/
public interface UserService {
/**
* 获取用户下所有信息
* @return
*/
List<UserEntity> listUser();
List<UserEntity> listUserByName(String name);
}
SpringMVCInterceptor
package com.spring.ssm.webutils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class SpringMVCInterceptor implements HandlerInterceptor {
/**
* preHandle方法是进行处理器拦截用的,顾名思义,该方法将在Controller处理之前进行调用,SpringMVC中的Interceptor拦截器是链式的,可以同时存在
* 多个Interceptor,然后SpringMVC会根据声明的前后顺序一个接一个的执行,而且所有的Interceptor中的preHandle方法都会在
* Controller方法调用之前调用。SpringMVC的这种Interceptor链式结构也是可以进行中断的,这种中断方式是令preHandle的返
* 回值为false,当preHandle的返回值为false的时候整个请求就结束了。
*
* 排除的url不会进入此方法
*
* 故跨域问题根据实际情况处理:
* 第一种:排除的url需要单独进行跨域处理
* 第二种:拦截器拦截所有接口进行跨域处理 再然后在拦截器中处理需重新排除的接口
*/
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
// TODO Auto-generated method stub
HttpSession session = request.getSession();
return true;
}
/**
* 这个方法只会在当前这个Interceptor的preHandle方法返回值为true的时候才会执行。postHandle是进行处理器拦截用的,它的执行时间是在处理器进行处理之
* 后,也就是在Controller的方法调用之后执行,但是它会在DispatcherServlet进行视图的渲染之前执行,也就是说在这个方法中你可以对ModelAndView进行操
* 作。这个方法的链式结构跟正常访问的方向是相反的,也就是说先声明的Interceptor拦截器该方法反而会后调用,这跟Struts2里面的拦截器的执行过程有点像,
* 只是Struts2里面的intercept方法中要手动的调用ActionInvocation的invoke方法,Struts2中调用ActionInvocation的invoke方法就是调用下一个Interceptor
* 或者是调用action,然后要在Interceptor之前调用的内容都写在调用invoke之前,要在Interceptor之后调用的内容都写在调用invoke方法之后。
*/
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
}
/**
* 该方法也是需要当前对应的Interceptor的preHandle方法的返回值为true时才会执行。该方法将在整个请求完成之后,也就是DispatcherServlet渲染了视图执行,
* 这个方法的主要作用是用于清理资源的,当然这个方法也只能在当前这个Interceptor的preHandle方法的返回值为true时才会执行。
*/
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
}
}
网友评论