代码之家  ›  专栏  ›  技术社区  ›  BHL

REST API中包含BASE64的大型数据集

  •  0
  • BHL  · 技术社区  · 4 年前

    所以我正在处理从数据库(MYSQL)中提取数据的网页

    enter image description here

    图像URL字段包含存储在服务器上的文件(JPG、PNG、PDF等)的位置。

    我正在尝试将base 64数据作为模型发送回前端(角度)

    cmPost。模型

        private id;
        private postType;
        private title;
        private description;
        private String category;
        private String location;
        private String eventStart;
        private String eventEnd;
        private String imageUrl;
        private String department;
        private Integer semester;
        private String options;
        private String settings;
        private String college;
        private Integer anonyomus;
        private Integer status;
        private String timestamp;
        private String uploadBy;
    

    上传助手。JAVA

     public List<cmPost> getUploadList(String email) throws IOException {
            List<cmPost> editBeforeSend = postRepo.getUploads(email);
    
            for (var i = 0; i < editBeforeSend.size(); i++) {
                System.out.println(editBeforeSend.get(i).getPostType());
                if(editBeforeSend.get(i).getPostType().toString().equalsIgnoreCase("NA")) {
                    String path = "uploads/events/" + editBeforeSend.get(i).getImageUrl();
                    String ext = FilenameUtils.getExtension(path);
                    String encoded = Base64.getEncoder().encodeToString(Files.readAllBytes(Path.of(path)));
                    editBeforeSend.get(i).setImageUrl("data:image/"+ext+";base64,"+encoded);
                }else if(editBeforeSend.get(i).getPostType().toString().equalsIgnoreCase("Papers") || editBeforeSend.get(i).getPostType().toString().equalsIgnoreCase("Projects")){
                    String path = "uploads/notes/" + editBeforeSend.get(i).getImageUrl();
                    String ext = FilenameUtils.getExtension(path);
                    String encoded = Base64.getEncoder().encodeToString(Files.readAllBytes(Path.of(path)));
                    editBeforeSend.get(i).setImageUrl("data:image/"+ext+";base64,"+encoded);
                }
            }
            return editBeforeSend;
        }
    

    上传控制器。JAVA

    @GetMapping("api/v1/upload/getPostList")
        public ResponseEntity<?> getPollsList(@RequestParam String email) throws IOException {
            List<cmPost> pollData = uploadHelper.getUploadList(email);
            return new ResponseEntity<>(pollData, HttpStatus.OK);
        }
    

    但我得到了431头大错误 ,在邮递员身上正常工作,但不是从前端角度,任何人都可以指出一种更好、更有效的方法来做到这一点。因为我将有很多图像文件,我需要在页面加载时发送到前端。

    在我的代码中犯了一些错误,现在已经修复了,工作正常

    我现在的问题是这是最好的方法吗??

    场景:如果我的数据库中有1000列具有映像路径的列,那么每个映像都将转换为BASE64并发送,那么什么是最佳和高效的方法

    任何指导都会非常有用

    提前谢谢。

    1 回复  |  直到 4 年前
        1
  •  1
  •   NoDataFound    4 年前

    我不是Angular方面的专家,但您可以像这样重新编写API:

    • 发送到API的http URL,例如: http://localhost/api/images/ID/POST_TYPE
    • 让API返回图像(例如: @GetMapping("/api/images/{id}/{postType}") )它将执行数据库查询。图像将作为二进制内容(而不是base64)返回。(见下文)

    您还可以发送缓存信息,以便浏览器不再下载图像。

    对于图像API,我没有看到您直接从文件系统使用图像。您可能只需在 /app/images 到存储它们的路径。

    • 发送到URI:/api/images/uploads/events/任何内容
    • 在/api/images之后阅读URI(我也不是SpringMVC专家,我不知道变量是否可以匹配整个路径)
    • 使用此URI获取实际文件
    • 使用 Files.copy(yourPath, request.getOutputStream()) 执行实际复制。

    注: 出于各种安全原因

    • 您还应该检查——特别是如果您使用来自外部的路径——该路径是否有效地位于您想要的位置。
    • 您还应该检查-给定的图像是个人数据-用户确实可以访问它。这是GRPD给的。