Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Rubén Ramírez
/
MangAffinity
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
abde01f1
authored
Mar 09, 2025
by
Rubén Ramírez
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
feat: [Fotos]: Añadidos configuraciones para trabajar con las fotos
parent
98238240
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
1 deletions
src/main/java/com/ujaen/tfg/mangaffinity/config/WebConfig.java
src/main/java/com/ujaen/tfg/mangaffinity/rest/UploadController.java
src/main/java/com/ujaen/tfg/mangaffinity/config/WebConfig.java
View file @
abde01f1
package
com
.
ujaen
.
tfg
.
mangaffinity
.
config
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.web.servlet.config.annotation.CorsRegistry
;
import
org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
;
...
...
@@ -9,9 +10,13 @@ public class WebConfig implements WebMvcConfigurer {
@Override
public
void
addResourceHandlers
(
ResourceHandlerRegistry
registry
)
{
// Usar una ruta relativa para servir archivos estáticos desde la carpeta uploads
registry
.
addResourceHandler
(
"/uploads/**"
)
.
addResourceLocations
(
"file:./uploads/"
);
}
@Override
public
void
addCorsMappings
(
CorsRegistry
registry
)
{
registry
.
addMapping
(
"/**"
).
allowedOrigins
(
"http://localhost:8080"
);
}
}
src/main/java/com/ujaen/tfg/mangaffinity/rest/UploadController.java
View file @
abde01f1
...
...
@@ -6,6 +6,14 @@ import org.springframework.http.ResponseEntity;
import
org.springframework.web.bind.annotation.*
;
import
java.net.MalformedURLException
;
import
java.nio.file.*
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.IOException
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
...
...
@@ -37,6 +45,31 @@ public class UploadController {
return
ResponseEntity
.
badRequest
().
build
();
}
}
@PostMapping
public
ResponseEntity
<
String
>
uploadFile
(
@RequestParam
(
"foto"
)
MultipartFile
foto
)
{
try
{
if
(
foto
.
isEmpty
())
{
return
ResponseEntity
.
status
(
HttpStatus
.
BAD_REQUEST
).
build
();
}
String
contentType
=
foto
.
getContentType
();
if
(
contentType
==
null
||
(!
contentType
.
equals
(
"image/jpeg"
)
&&
!
contentType
.
equals
(
"image/png"
)))
{
return
ResponseEntity
.
status
(
HttpStatus
.
BAD_REQUEST
).
build
();
}
if
(!
Files
.
exists
(
uploadDir
))
{
Files
.
createDirectories
(
uploadDir
);
}
Path
path
=
uploadDir
.
resolve
(
foto
.
getOriginalFilename
());
Files
.
copy
(
foto
.
getInputStream
(),
path
,
StandardCopyOption
.
REPLACE_EXISTING
);
return
ResponseEntity
.
ok
(
foto
.
getOriginalFilename
());
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
return
ResponseEntity
.
status
(
HttpStatus
.
INTERNAL_SERVER_ERROR
).
build
();
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment