67,513
社区成员
发帖
与我相关
我的任务
分享
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
@Value("${moregs-oss.project-name}")
private String project;
@Value("${moregs-oss.root-path}")
private String rootPath;
/**
* 自动转换时间格式
*
* @param registry date
*/
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addFormatter(new DateFormatter("yyyy-MM-dd HH:mm:ss"));
}
/**
* 资源映射路径
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(project + "/**").addResourceLocations(rootPath);
}
}
/**
*
* @param headImg
* @param route
* headImg 是获取用户头像的路由, pushinfo 是获取推流地址的二维码的路由
* @param response
* @throws IOException
*/
@ResponseBody
@RequestMapping(value = "/showImage", method = RequestMethod.GET)
public void showImage(@RequestParam(value = "imgName") String imgName, @RequestParam(value = "route") String route, HttpServletResponse response) throws IOException {
response.setContentType("text/html; charset=UTF-8");
response.setContentType("image/jpeg");
String parentPath = FileUtils.getBaseFilePath() + "/" + route;
FileUtils.createParentPath(parentPath);
String fullFileName = parentPath + "/" + imgName;
logger.debug("showImage fullFileName is :{}", fullFileName);
FileInputStream fis = new FileInputStream(fullFileName);
OutputStream os = response.getOutputStream();
try {
int count = 0;
byte[] buffer = new byte[1024 * 1024];
while ((count = fis.read(buffer)) != -1) {
os.write(buffer, 0, count);
}
os.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (os != null) {
os.close();
}
if (fis != null) {
fis.close();
}
}
}