I am trying to display some documents on a page. I have saved them in the database but I am not sure how to go on displaying them on the website and make them downloadable. The files have the format (pdf, doc, ppt). If anyone could suggest something, I'd be grateful.
This is the File Entity:
@Entity
@Table(name = "chapter_files")
public class File {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "file_id")
private Long id;
@Column(name = "file_name")
private String fileName;
@Column(name = "file")
@Lob
private byte[] file;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="chapter_id")
private Chapter chapter;
}
This is the MySQL table create query:
CREATE TABLE `chapter_files` (
`file_id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`file_name` VARCHAR(50) NOT NULL,
`file` LONGTEXT NOT NULL,
`chapter_id` BIGINT(20) NOT NULL,
PRIMARY KEY (`file_id`),
INDEX `FKChapter` (`chapter_id`),
CONSTRAINT `FKChapter` FOREIGN KEY (`chapter_id`) REFERENCES `chapters` (`chapter_id`)
)
This is the Service class method to save the files in the database:
public File saveFiles(MultipartFile file, Chapter chapter) {
File doc = new File();
String fileName = StringUtils.cleanPath(file.getOriginalFilename());
doc.setFileName(fileName);
try {
doc.setFile(file.getBytes());
doc.setChapter(chapter);
} catch (IOException e) {
e.printStackTrace();
}
return fileRepository.save(doc);
}
I am not sure how I should display the files on the page and make them downloadable. If you have any suggestions or any articles I could read, I'd gladly follow.