springboot限制文件访问

63 阅读1分钟

1.使用SpringBoot中的处理器

2.根据路径返回流

public void backResponse(HttpServletResponse response,String contentType,String filePath) { FileReader fileReader = new FileReader(filePath); byte[] data = fileReader.readBytes(); response.setContentType(contentType); try (OutputStream out= response.getOutputStream() ){ out.write(data); out.flush(); } catch (IOException e) { e.printStackTrace(); } }