i’m working on a web application with angular and springboot where i can upload files and store them in the app , i did work on sending mails through my web app but im having trouble fetching the file from the already uploaded and stored files . also the document has to be encrypted while being sent, i’m still a beginner and i did a lot of research and tried a bunch of solutions but nothing works.
package project.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import project.exceptions.FileStorageException;
import project.models.FileUploadEntity;
import project.repository.FileUploadRepository;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Objects;
@Service("FileUploadService")
public class FileUploadService {
private final FileUploadRepository fileUploadRepository;
private Path uploadLocation;
@Autowired
public FileUploadService(FileUploadRepository fileUploadRepository, Environment environment) {
this.fileUploadRepository = fileUploadRepository;
String uploadDir = environment.getProperty("file.upload.dir");
if (uploadDir == null) {
throw new IllegalArgumentException("File upload directory is not configured");
}
this.uploadLocation = Paths.get(uploadDir).toAbsolutePath().normalize();
try {
Files.createDirectories(this.uploadLocation);
} catch (Exception ex) {
throw new FileStorageException("Could not create directory", ex);
}
}
public FileUploadEntity uploadFile(String ownedBy, String description, MultipartFile file) throws IOException {
String originalFileName = StringUtils.cleanPath(Objects.requireNonNull(file.getOriginalFilename()));
Path targetLocation = this.uploadLocation.resolve(originalFileName);
Files.copy(file.getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING);
FileUploadEntity theFile = new FileUploadEntity();
theFile.setOwnedBy(ownedBy);
theFile.setDescription(description);
theFile.setType(file.getContentType());
theFile.setName(originalFileName);
theFile.setFile(file.getBytes());
theFile.setUploadDir(String.valueOf(this.uploadLocation));
return fileUploadRepository.save(theFile);
}
public String deleteFileById(Long fileId) throws FileNotFoundException {
FileUploadEntity fileToDelete = fileUploadRepository.findById(fileId)
.orElseThrow(() -> new FileNotFoundException("File with ID " + fileId + " not found"));
Path filePath = uploadLocation.resolve(fileToDelete.getName());
try {
Files.deleteIfExists(filePath);
fileUploadRepository.delete(fileToDelete);
return "File with ID " + fileId + " deleted successfully";
} catch (IOException e) {
if (e instanceof FileNotFoundException) {
throw new FileNotFoundException("File with ID " + fileId + " not found at specified path.");
} else {
throw new FileStorageException("Failed to delete file", e);
}
}
}
}
this is my file upload service
package project.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.mail.internet.MimeMessage;
@Service
public class MailServiceImpl implements MailService.EmailService{
@Autowired
private JavaMailSender javaMailSender;
@Value("${spring.mail.username}")
private String from;
@Override
public String sendMail(MultipartFile[] file, String to, String[] cc, String subject, String body) {
try {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setFrom(from);
mimeMessageHelper.setTo(to);
mimeMessageHelper.setCc(cc);
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setText(body);
for (int i = 0; i < file.length; i++) {
mimeMessageHelper.addAttachment(
file[i].getOriginalFilename(),
new ByteArrayResource(file[i].getBytes()));
}
javaMailSender.send(mimeMessage);
return "mail sent";
} catch (Exception e) {
throw new RuntimeException(e); // Consider more specific exception handling
}
}
}
and this is the mail service (everything is configured and it works just fine , i just need to fetch the document from the already uploaded documents instead of the device