Java实现图片传输给前端展示(接口方式)

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

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;
}


版权声明:本文为m0_50008952原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。