Java OSS图片预览接口

网关统一鉴权方式

HeaderAuthorization鉴权

@GetMapping(value = "/image/view")
public void downloadDoc(@RequestParam("filePath") String filePath, HttpServletResponse response) {

    log.info("download image. filePath={}", filePath);
    try (OutputStream outputStream = response.getOutputStream()){
        Result<byte[]> file = fileRpcApi.download(filePath);
        if (file.fail()) {
            response.getOutputStream().write(JSON.
                    toJSONString(ResultBuilder.failure(ResponseCode.NOT_FOUND, "file not found"))
                    .getBytes());
            return;
        }
        response.setContentType("image/jpeg;charset=utf-8");
        outputStream.write(file.getData());
    } catch (Exception e) {
        log.error("", e);
    }
}
@GetMapping(value = "/image/viewMulti")
@SuppressWarnings("unchecked")
public Result<List<String>> viewDoc(@RequestParam(value = "filePath", required = false) String filePath) {
    if (isBlank(filePath)) {
        return ResultBuilder.failure(ResponseCode.BAD_REQUEST, "filePath is empty!");
    }
    List<String> list = new ArrayList<>(3);
    Arrays.stream(filePath.split(",|;")).map(p -> p.trim()).forEach(p -> {
        Result<byte[]> file = fileRpcApi.download(p);
        if (file.fail()) {
            log.warn("file[{}] download fail", p);
            return;
        }
        list.add(Base64.getEncoder().encodeToString(file.getData()));
    });

    return ResultBuilder.success(list);
}

token鉴权方式

如:https://www.appblog.cn/image?token=xxxxxx

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/25/java-oss-image-preview-interface/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Java OSS图片预览接口
网关统一鉴权方式 如Header中Authorization鉴权 @GetMapping(value = "/image/view") public void downloadDoc(@RequestParam("filePath"……
<<上一篇
下一篇>>
文章目录
关闭
目 录