Je rencontre une erreur 500 lors du traitement des images et des vidéos dans mon projet. Ce n'est pas une erreur de protection, car les autres formulaires fonctionnent correctement. Cependant, il semble y avoir un problème spécifique à la gestion des médias.
Afin de faciliter la maintenance du projet à long terme, j'ai choisi une structure simple. Mon projet utilise Angular pour le frontend et Spring Boot pour le backend. Les fichiers multimédias (images et vidéos) sont tous stockés dans le dossier public d'Angular, et Spring Boot les charge directement à partir de ce dossier.
@Service
public class ImageService {
private final String uploadDir = "../public/images/"; // Chemin vers le répertoire public d'Angular
private final String uploadDirVideos = "../public/videos/";
public String uploadImage(MultipartFile file) throws IOException {
if (file.isEmpty()) {
throw new IllegalStateException("Cannot upload empty file.");
}
// Vérifie si le répertoire existe, sinon il le crée
Path uploadPath = Paths.get(uploadDir);
if (!Files.exists(uploadPath)) {
Files.createDirectories(uploadPath); // Crée le répertoire s'il n'existe pas
}
String originalFileName = file.getOriginalFilename();
String fileExtension = originalFileName.substring(originalFileName.lastIndexOf("."));
String newFileName = UUID.randomUUID().toString() + fileExtension;
Path filePath = uploadPath.resolve(newFileName);
Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
return newFileName; // Retourne le nouveau nom du fichier pour l'enregistrer dans l'entité Category
}
public String uploadVideo(MultipartFile file) throws IOException {
if (file.isEmpty()) {
throw new IllegalStateException("Cannot upload empty file.");
}
// Vérifie si le répertoire existe, sinon il le crée
Path uploadPath = Paths.get(uploadDirVideos);
if (!Files.exists(uploadPath)) {
Files.createDirectories(uploadPath); // Crée le répertoire s'il n'existe pas
}
String originalFileName = file.getOriginalFilename();
String fileExtension = originalFileName.substring(originalFileName.lastIndexOf("."));
String newFileName = UUID.randomUUID().toString() + fileExtension;
Path filePath = uploadPath.resolve(newFileName);
Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
return newFileName; // Retourne le nouveau nom du fichier pour l'enregistrer
}
public byte[] getImage(String fileName) throws IOException {
Path filePath = Paths.get(uploadDir + fileName);
return Files.readAllBytes(filePath);
}
public byte[] getVideo(String fileName) throws IOException {
Path filePath = Paths.get(uploadDirVideos + fileName);
return Files.readAllBytes(filePath);
}
}
nginx
server {
listen 80;
server_name cognitiex.com www.cognitiex.com;
# Rediriger toutes les requêtes HTTP vers HTTPS
return 301 https://$host$request_uri;
}
server {
listen 443 ssl; # Écouter sur le port 443 pour SSL
server_name cognitiex.com; # Domaine sans www
ssl_certificate /etc/letsencrypt/live/cognitiex.com/fullchain.pem; # Chemin vers le certificat
ssl_certificate_key /etc/letsencrypt/live/cognitiex.com/privkey.pem; # Chemin vers la clé privée
return 301 https://www.cognitiex.com$request_uri; # Redirection vers www.cognitiex.com
}
server {
listen 443 ssl; # Écouter sur le port 443 pour SSL
server_name www.cognitiex.com; # Domaine avec www
ssl_certificate /etc/letsencrypt/live/cognitiex.com/fullchain.pem; # Chemin vers le certificat
ssl_certificate_key /etc/letsencrypt/live/cognitiex.com/privkey.pem; # Chemin vers la clé privée
root /var/www/ecomme/dist/accounts/browser; # Chemin vers les fichiers
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html; # Sert les fichiers Angular
}
location /api/ {
proxy_pass http://localhost:8080/api/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
add_header 'Access-Control-Allow-Origin' 'https://www.cognitiex.com' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
}
location /api/videos/ {
alias /var/www/ecomme/public/videos/; # Notez la barre oblique
}
location /api/images/ {
alias /var/www/ecomme/public/images/; # Notez la barre oblique
}
error_page 403 /403.html; # Gestion des erreurs
location = /403.html {
internal;
}
}
Problème dans la gestion des médias avec Spring Boot et Angular
Bienvenue sur OVHcloud Community
Posez des questions, recherchez des informations, publiez du contenu et interagissez avec d'autres membres d'OVHcloud Community.
question
Problème dans la gestion des médias avec Spring Boot et Angular
Avis positifs (0)
212 Vues
Sujets apparentés
- Accès FTP sur VPS
18715
30.06.2017 18:35
- Problème page site en construction
11874
17.02.2019 18:15
- Migration de Debian 8 vers Debian 9
10199
24.06.2017 07:59
- Commande restart mysql Centos 7
9924
11.04.2017 15:05
- Tuto pour installer Wordpress sur un VPS OVH
9751
16.10.2016 15:52
- Erreur "502 Bad Gateway - nginx"
7426
06.12.2016 08:39
- Mode rescue modifier firewall URGENT
6510
07.09.2018 11:38
- Non réception des e-mails
6390
30.12.2017 21:41
- OpenVPN sur VPS ne fonctionne pas
6068
06.08.2018 10:33
- Fin de l'Hébergement web Plesk
5963
13.12.2020 21:44