按Blob显示图像不正确

时间:2020-01-13 09:36:04

标签: java reactjs spring-boot ftp-server

我创建了一个用于上传图像的API和一个用于通过Resource(FTP服务器和Spring Boot)获取图像的API。在前端,我使用reactjs并通过base64显示图像。但是我上传的某些图像无法显示(主要是.png)。当我对该图像进行响应时,将其转换为base64,然后转换为图像,但不会显示。

  • 上传:

    • Ftp商店:
    @Override
    public boolean storeFile(String filePath, InputStream fileIs) {
        FileStoreConnectInfo connectInfo = new FileStoreConnectInfo(appProperties);
        try (FtpService ftpService = new FtpService(connectInfo)) {
            return ftpService.store(filePath, fileIs);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }
  • 通过资源获取图片:

    • 控制器:
    @GetMapping("/me/profile/avatar/download")
    public ResponseEntity<Resource> download(@RequestParam   String path){
        ImageResponse response = userProfileService.download(path);
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(response.getResource());
    }
  • ImageResponse:
   @Getter @Setter
public class ImageResponse {
    private Resource resource;

    public ImageResponse() {}
}

  • 实施:
    public ImageResponse download(String avatarUrl) {
        InputStream fis = userProfileStoreService.getFile(path + avatarUrl);
        InputStreamResource resource = new InputStreamResource(fis);
        ImageResponse response = new ImageResponse();
        response.setResource(resource);
        return response;
    }

  • FTP获取文件:
        FileStoreConnectInfo connectInfo = new FileStoreConnectInfo(appProperties);
        try (FtpService ftpService = new FtpService(connectInfo)) {
            return ftpService.retrieveFileStream(filePath);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

0 个答案:

没有答案