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;
}
}
VPS-old - Problème dans la gestion des médias avec Spring Boot et Angular
Willkommen in der OVHcloud Community
Stellen Sie Fragen, suchen Sie nach Informationen, veröffentlichen Sie Inhalte und interagieren Sie mit anderen Mitgliedern der OVHcloud Community.
Frage
Problème dans la gestion des médias avec Spring Boot et Angular
Positive Bewertungen (0)
2995 Ansichten
Related questions
- Accès FTP sur VPS
34173
30.06.2017 18:35
- Ma Webradio : Icecast2 ok?, Butt envoi, mais aucun Player fonctionne
27651
14.01.2021 21:37
- Problème page site en construction
23023
17.02.2019 18:15
- Commande restart mysql Centos 7
22775
11.04.2017 15:05
- Migration de Debian 8 vers Debian 9
20038
24.06.2017 07:59
- Erreur "502 Bad Gateway - nginx"
19303
06.12.2016 08:39
- Tuto pour installer Wordpress sur un VPS OVH
17116
16.10.2016 15:52
- Mode rescue modifier firewall URGENT
16861
07.09.2018 11:38
- OpenVPN sur VPS ne fonctionne pas
16053
06.08.2018 10:33
- Toujours pas de SLA ?
16021
09.01.2018 18:03