how to display image in a browser in spring boot using the image url

1.7k Views Asked by At

I'm saving images to the database using spring boot, the image url is also displayed on postman when I send it but the issue is that when I copy the image url link to the browser, the image is not showing rather it's showing This application has no explicit mapping for /error, so you are seeing this as a fallback.What is wrong with it.

This is my Entity class

@Data
@Entity
@NoArgsConstructor
public class Attachment {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String fileName;
    private String fileType;
    @Lob
    private byte[] data;

    public Attachment(String fileName, String fileType, byte[] data) {
        this.fileName = fileName;
        this.fileType = fileType;
        this.data = data;
    }
}

This is the dto class

@Data
@NoArgsConstructor
@AllArgsConstructor
public class AttachmentDto {

    private String fileName;
    private String downloadUrl;
    private String fileType;
    private long fileSize;
}
        

This is my controller class

@RestController
public class AttachmentController {


    private AttachmentService attachmentService;

    public AttachmentController(AttachmentService attachmentService) {
        this.attachmentService = attachmentService;
    }


@PostMapping("/upload")
    public ResponseEntity<AttachmentDto> uploadFile(@RequestParam("file")MultipartFile file) throws Exception {
        Attachment attachment = attachmentService.saveFile(file);
        String downloadUrl = ServletUriComponentsBuilder.fromCurrentContextPath()
                .path("/profileImage")
                .path(attachment.getFileName())
                .toUriString();

        return new ResponseEntity<>(new AttachmentDto(attachment.getFileName(),downloadUrl,file.getContentType(),file.getSize()), HttpStatus.CREATED);
    }
}
        

Here is my service class

@Service
public class AttachmentService {


    private AttachmentRepository attachmentRepository;

    public AttachmentService(AttachmentRepository attachmentRepository) {
        this.attachmentRepository = attachmentRepository;
    }

    public Attachment saveFile(MultipartFile file) throws Exception {
        String fileName = StringUtils.cleanPath(file.getOriginalFilename());
        try {
            if (fileName.contains("..")){
                throw new Exception("File Name contains invalid sequence");
            }
            Attachment attachment = new Attachment(fileName,file.getContentType(),file.getBytes());
            return attachmentRepository.save(attachment);
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception("Could not save file "+fileName);

        }
    }
}

The image was saved successfully in postman and this was returned

"fileName": "IMG_20180312_131559.jpg",
    "downloadUrl": "http://localhost:8080/profileImageIMG_20180312_131559.jpg",
    "fileType": "image/jpeg",
    "fileSize": 1061724

I copied the downloadUrl into the browser but the image is not showing nor downloading but rather showing This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Sep 26 11:11:52 PDT 2022
There was an unexpected error (type=Not Found, status=404).
0

There are 0 best solutions below