SpringBoot
Controlller代码:
@RequestMapping("/tb-user")
@GetMapping(value = "imgInfo",produces = MediaType.IMAGE_JPEG_VALUE)
public byte[] imgInfo(String path) throws Exception {
byte[] bs=null;
File file = new File(path);
if (file.exists()) {
FileInputStream inputStream = new FileInputStream(file);
bs = new byte[inputStream.available()];
inputStream.read(bs, 0, inputStream.available());
return bs;
}
return bs;
}
前端代码:
Windows中文件的命名是“\”在这里我们需要替换为“/”
<el-image :src="byteToArray(path)"/>
.........
byteToArray(path){
let reg = new RegExp('\\\\',"g")//g,表示全部替换。
path=path.replace(reg,"/");
return "http://localhost:8080/tb-user/imgInfo?path="+path
},
配置文件中加上:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable().authorizeRequests()
.antMatchers(HttpMethod.GET, "/product/imgInfo").permitAll()
.anyRequest().authenticated()
.and()
.addFilter(new JWTLoginFilter(authenticationManager()))
.addFilter(new JWTAuthenticationFilter(authenticationManager()))
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.exceptionHandling()
.authenticationEntryPoint(new JWTAuthenticationEntryPoint());
}