请求后,可以直接显示图片和文件内容
@RequestMapping("/resource/**")
@ResponseBody
public ResponseEntity<?> getResource(HttpServletRequest request) {
String requestUrl = request.getRequestURI();
requestUrl = requestUrl.substring("/resource".length());
String file = this.getRootPath() + requestUrl; //文件路径
byte[] data = new byte[0];
try {
data = FileUtils.readFileToByteArray(new File(file)); //转换为字节流
} catch (IOException e) {
e.printStackTrace();
}
return ResponseEntity.ok(data);
}其中FileUtils是Apache Commons IO的API

本文介绍了一种通过@RequestMapping注解实现的代码复用方法,可以在接收到特定请求时直接展示图片和文件内容。该方法使用了Apache Commons IO的FileUtils工具将文件转换为字节流进行返回。
929

被折叠的 条评论
为什么被折叠?



