我创建了一个用于上传图像的API和一个用于通过Resource(FTP服务器和Spring Boot)获取图像的API。在前端,我使用reactjs并通过base64显示图像。但是我上传的某些图像无法显示(主要是.png)。当我对该图像进行响应时,将其转换为base64,然后转换为图像,但不会显示。
@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());
}
@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;
}
FileStoreConnectInfo connectInfo = new FileStoreConnectInfo(appProperties);
try (FtpService ftpService = new FtpService(connectInfo)) {
return ftpService.retrieveFileStream(filePath);
} catch (Exception e) {
throw new RuntimeException(e);
}