1、resource static下有个图片,希望浏览器可以查看这个图片
访问:
若yml设置路径,则可以定义在static下才可以访问
classpath代表类路径,都在target下
也就是项目在运行后的resource下的文件都会到classes下去
无需在target下创建文件夹。在resource下创建文件夹即可
spring.mvc.static-path-pattern=/static/**
spring.mvc.static-path-pattern=/static/**
spring.web.resources.static-locations=classpath:/css
2、文件上传13:00
package com.example.helloworld.controller;
import org.springframework.web.bind.annotation.PostMapping;
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;
/**
* @Author Fxdll
* @Date 2024/4/30 23:05
* @PackageName:com.example.helloworld.controller
* @ClassName: FileController
* @Description: TODO
* @Version 1.0
*/
@RestController
public class FileUploadController {
@PostMapping("/upload")
//等价于@RmoteMapping(value="/uplaod",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());
// System.out.println(System.getProperty("user.dir"));
//获取项目的路径
String path = request.getServletContext().getRealPath("/upload/");
//获取上传文件的真实路径
System.out.println(path);
//保存文件
saveFile(photo,path);
// TODO: 返回上传成功消息
return "文件上传成功";
}
private void saveFile(MultipartFile photo,String path)throws IOException {
//判断路径是否存在,不存在则创建
File Dir = new File(path);
if (!Dir.exists()) {
//创建目录
Dir.mkdirs();
}
File file = new File(path + photo.getOriginalFilename());
photo.transferTo(file);
}
}
3、如何让上传的图片也可以访问?
配置静态路径即可
第一个是设置单词上传大小上线
第二个是访问图片静态路径设置
spring.servlet.multipart.max-file-size=10MB spring.web.resources.static-locations=class:/upload/
4、拦截器
5、拦截器定义
package com.example.helloworld.config;
import com.example.helloworld.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @Author Fxdll
* @Date 2024/5/3 21:33
* @PackageName:com.example.helloworld.config
* @ClassName: Webconfig
* @Description: TODO
* @Version 1.0
*/
@Configuration
public class Webconfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/user/**");
}
}
package com.example.helloworld.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @Author Fxdll
* @Date 2024/4/30 22:31
* @PackageName:com.example.helloworld.interceptor
* @ClassName: LoginInterceptor
* @Description: TODO 拦截器
* @Version 1.0
*/
/**
* request:
* 1.获取请求信息(客户端传来的cooke,url参数,请求体)
* response:
* 1.设置响应信息
*/
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("LoginInterceptor");
return true;
}
}