前端调用此接口获取图片:

public void getPersonPic(String picName, HttpServletResponse response) throws IOException {
    ServletOutputStream outputStream = null;
    try {
        byte[] bytes = getPic(picName);
        outputStream = response.getOutputStream();
        outputStream.write(bytes);
        outputStream.flush();
    } catch (IOException e) {
        e.printStackTrace();
        if (outputStream != null) {
            outputStream.close();
        }
    }
}

public byte[] getPic(String picName) {
    //进行图片路径处理。拼接上图片名
    final Path path = Path.of(System.getProperty("user.dir"),"/home","/pic",picName);
    byte[] bytes = null;
    if (path.toFile().exists()) {
        try {
            bytes = Files.readAllBytes(path);
        } catch (Exception e) {
        }
    }else {}
    return bytes;
}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐