java Springboot02--Controller,文件上传,拦截器

 因为前后端分离了,所以这个项目基本用不到@controller

 

 

 

 

这句话意思:

controller只能用get接受前端的请求

@RequestMapping(value = "/hello",method = RequestMethod.GET)
@GetMapping("/hello")

这两句等价的

前段传递参数,我怎么接收呢?

 ?nickname=hy是前端传递的参数

hello里面的形参nickname需要和他保持一致,接收这个参数

 注意点:

1,传递的和接收的参数名不一致,用@requestparam注解强制绑定,但是绑定后这个参数前端就必须要传递,可以加一个参数将其设为可选的

POST请求

嗯前段要发送post请求需要一些复杂的组件,我们可以用软件模拟,这里用到apipost

 如果是json格式传输数据呢?

1. 用requestbody包起来参数

2. 传递的参数是一个类的实例,这个类的一些属性要和你传递的参数一致

@RequestMapping(value = "/postTest2",method = RequestMethod.POST)
    public String post2(@RequestBody User user){
        System.out.println(user);
        return "Post请求,接收json数据";
    }

user类:

package com.example.demo.entity;

public class User {
    private String name;
    private String password;

    public String getName() {
        return name;
    }

    public String getPassword() {
        return password;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}


通配符

用的不多,以后用到了解吧

2. 静态资源访问

 如何使用:比如这边有一张鼠鼠的图片,用户想通过浏览器访问到,静态资源默认斜杠后面可以访问到,即在浏览器输入

http://localhost:8080/shushu.jpg

 如果你不想在根目录下,默认是/**,可以设置虚拟映射

spring.mvc.static-path-pattern=/images/**
这是更改默认路径
spring.web.resources.static-locations=classpath:/css
这个css是自己创建的目录,classpath是类目录下,是target/classes目录下

3. 文件上传

需要前端上传文件

 

package com.example.demo.Controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@RestController
public class FileUploadController {
    @PostMapping("/upload")
    //这玩意儿和下面的等价
    //@RequestMapping(value = "/upload",method = RequestMethod.POST)
    public String up(String nickname, MultipartFile photo, HttpServletRequest request) throws IOException{
        System.out.println(nickname);
        System.out.println(photo.getOriginalFilename());

        System.out.println(photo.getContentType());

        String path = request.getServletContext().getRealPath("/upload/");
        //这句话的意思是获取到云端服务器的路径
        //怎么获取的:用上面那个参数HttpServletRequest request一个请求
        //request.getServletContext().getRealPath这是一个动态路径,后面的可以任意拼接
        System.out.println(path);
        saveFile(photo,path);
        return "上传成功";
    }

    public void saveFile(MultipartFile photo, String path) throws IOException{
        File dir = new File(path);
        if(!dir.exists()){
            dir.mkdir();
        }

        File file = new File(path + photo.getOriginalFilename());
        photo.transferTo(file);
        //这句话意思是把photo也就是网上那张图存储到file当中
        //那用户怎么从浏览器访问这个目录呢?配置一下那个静态资源目录即可
        //spring.web.resources.static-locations=/upload/
        //斜线就是tomcat运行的云端根目录
    }
}

配置

spring.web.resources.static-locations=/upload/
# 斜线就是tomcat运行的云端根目录

4. 拦截器

 

 

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("LoginInterceptor");
        return true;
    }
}

 配置一个config

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/user/**");
        //表示拦截哪些路径
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值