1

我正在使用 spring boot 创建 REST API,并计划做客户端(移动)端。我想知道返回和接收文件(图像等)的最佳方式是什么,因为当我返回带有二进制数组作为字段的对象时:

@GetMapping("/downloadProfile/{fileId}")
        public Profile downloadProfile(@PathVariable String fileId) {
            Profile profile = profileService.findById(Long.parseLong(fileId));

            return profile;
        }

……

@Entity
@Table(name = "profiles")
public class Profile {

    @Id
    @GeneratedValue
    private Long id;

    private String firstName;

    private String lastName;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "image_id")
    private Image image;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "location_id")
    private Location location;
    ...

我得到 JSON:

{
    "id": 1,
    "firstName": "May",
    "lastName": "Clark",
    "image": {
        "fileName": "dog.jpg",
        "fileType": "image/jpeg",
        "data": "/9j/4AAAQsk ...

它是在 Base64 中自动编码还是只是二进制数据,如果是这样,我应该将它以编码格式存储在 DB 中吗?在客户端上发送简单工作的正确方法是什么?

4

0 回答 0