Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
BackEnd-V1-JasperReport
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Anderson Zambrano
BackEnd-V1-JasperReport
Commits
b5c87a67
Commit
b5c87a67
authored
May 18, 2023
by
Joel Andres Molina Velez
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Subida de archivos
parent
e4b94072
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
116 additions
and
67 deletions
+116
-67
FileContreller.java
...espe/movilidad/MovilidadWS/controller/FileContreller.java
+56
-0
FileServiceImpl.java
...u/espe/movilidad/MovilidadWS/service/FileServiceImpl.java
+0
-57
IFileService.java
.../edu/espe/movilidad/MovilidadWS/service/IFileService.java
+9
-8
ServiceFile.java
...c/edu/espe/movilidad/MovilidadWS/service/ServiceFile.java
+49
-0
application-local.properties
src/main/resources/application-local.properties
+2
-2
No files found.
src/main/java/ec/edu/espe/movilidad/MovilidadWS/controller/FileContreller.java
0 → 100644
View file @
b5c87a67
package
ec
.
edu
.
espe
.
movilidad
.
MovilidadWS
.
controller
;
import
ec.edu.espe.movilidad.MovilidadWS.model.FileModel
;
import
ec.edu.espe.movilidad.MovilidadWS.service.IFileService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder
;
import
org.springframework.core.io.Resource
;
import
java.util.List
;
import
java.util.stream.Collectors
;
import
static
ec
.
edu
.
espe
.
movilidad
.
MovilidadWS
.
Constant
.
GlobalConstants
.
V1_API_VERSION
;
@RestController
@CrossOrigin
(
origins
=
"*"
)
@RequestMapping
(
V1_API_VERSION
+
"/file"
)
public
class
FileContreller
{
@Autowired
private
IFileService
fileService
;
//Subir el archivo
@PostMapping
(
"/upload"
)
public
ResponseEntity
<
String
>
uploadFiles
(
@RequestParam
(
"files"
)
List
<
MultipartFile
>
files
){
try
{
fileService
.
save
(
files
);
return
ResponseEntity
.
status
(
HttpStatus
.
OK
).
body
(
"Los archivos fueron cargados al servidor"
);
}
catch
(
Exception
e
){
return
ResponseEntity
.
status
(
HttpStatus
.
INTERNAL_SERVER_ERROR
).
body
(
"Ocurrio un error al subir el archivo"
);
}
}
// Leer el archivo por el nombre
@GetMapping
(
"/{filename:.+}"
)
public
ResponseEntity
<
Resource
>
getFile
(
@PathVariable
String
filename
)
throws
Exception
{
Resource
resource
=
fileService
.
load
(
filename
);
return
ResponseEntity
.
ok
()
.
header
(
HttpHeaders
.
CONTENT_DISPOSITION
,
"attachment; filename=\""
+
resource
.
getFilename
()
+
"\""
)
.
body
(
resource
);
}
//Obtener todos los archivos
@GetMapping
(
"/fileAll"
)
public
ResponseEntity
<
List
<
FileModel
>>
getAllFiles
()
throws
Exception
{
List
<
FileModel
>
files
=
fileService
.
loadAll
().
map
(
path
->
{
String
filename
=
path
.
getFileName
().
toString
();
String
url
=
MvcUriComponentsBuilder
.
fromMethodName
(
FileContreller
.
class
,
"getFile"
,
path
.
getFileName
().
toString
()).
build
().
toString
();
return
new
FileModel
(
filename
,
url
);
}).
collect
(
Collectors
.
toList
());
return
ResponseEntity
.
status
(
HttpStatus
.
OK
).
body
(
files
);
}
}
src/main/java/ec/edu/espe/movilidad/MovilidadWS/service/FileServiceImpl.java
deleted
100644 → 0
View file @
e4b94072
package
ec
.
edu
.
espe
.
movilidad
.
MovilidadWS
.
service
;
import
org.springframework.core.io.UrlResource
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.annotation.Resource
;
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.util.stream.Stream
;
@Service
public
class
FileServiceImpl
implements
FileService
{
private
final
Path
root
=
Paths
.
get
(
"uploads"
);
@Override
public
void
init
()
{
try
{
Files
.
createDirectory
(
root
);
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
"No se puede iniciar el storage"
);
}
}
@Override
public
void
save
(
MultipartFile
file
)
{
try
{
Files
.
copy
(
file
.
getInputStream
(),
this
.
root
.
resolve
(
file
.
getOriginalFilename
()));
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
"No se pudo guardar el archivo"
);
}
}
@Override
public
Resource
load
(
String
filename
)
{
return
null
;
}
@Override
public
void
deleteAll
()
{
}
@Override
public
Stream
<
Path
>
loadAll
()
{
return
null
;
}
@Override
public
String
deleteFile
(
String
filename
)
{
return
null
;
}
}
src/main/java/ec/edu/espe/movilidad/MovilidadWS/service/FileService.java
→
src/main/java/ec/edu/espe/movilidad/MovilidadWS/service/
I
FileService.java
View file @
b5c87a67
package
ec
.
edu
.
espe
.
movilidad
.
MovilidadWS
.
service
;
import
org.springframework.core.io.Resource
;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.annotation.Resource
;
import
java.nio.file.Path
;
import
java.util.List
;
import
java.util.stream.Stream
;
public
interface
FileService
{
public
void
init
();
//método para guardar el archivo
public
interface
IFileService
{
public
void
save
(
MultipartFile
file
);
//MultipartFile resive varios archivos
public
Resource
load
(
String
filename
);
public
void
save
(
MultipartFile
file
)
throws
Exception
;
//MultipartFile resive varios archivos
public
void
deleteAll
();
public
Resource
load
(
String
name
)
throws
Exception
;
public
void
save
(
List
<
MultipartFile
>
file
)
throws
Exception
;
public
Stream
<
Path
>
loadAll
()
throws
Exception
;
public
Stream
<
Path
>
loadAll
();
public
String
deleteFile
(
String
filename
);
}
src/main/java/ec/edu/espe/movilidad/MovilidadWS/service/ServiceFile.java
0 → 100644
View file @
b5c87a67
package
ec
.
edu
.
espe
.
movilidad
.
MovilidadWS
.
service
;
import
org.springframework.core.io.UrlResource
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.core.io.Resource
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.util.List
;
import
java.util.stream.Stream
;
@Service
public
class
ServiceFile
implements
IFileService
{
private
final
Path
rootFolder
=
Paths
.
get
(
"uploads"
);
@Override
public
void
save
(
MultipartFile
file
)
throws
Exception
{
Files
.
copy
(
file
.
getInputStream
(),
this
.
rootFolder
.
resolve
(
file
.
getOriginalFilename
()));
//se envía el archivo con el nombre original a la carpeta
}
@Override
public
Resource
load
(
String
name
)
throws
Exception
{
Path
file
=
rootFolder
.
resolve
(
name
);
//busca el archivo dentro de la carpeta upload
Resource
resource
=
(
Resource
)
new
UrlResource
(
file
.
toUri
());
return
resource
;
}
@Override
public
void
save
(
List
<
MultipartFile
>
files
)
throws
Exception
{
for
(
MultipartFile
file:
files
){
//guardar varios archivos
this
.
save
(
file
);
}
}
@Override
public
Stream
<
Path
>
loadAll
()
throws
Exception
{
//Obtenemos todos los archivos de la carpeta uploads
return
Files
.
walk
(
rootFolder
,
1
).
filter
(
path
->
!
path
.
equals
(
rootFolder
)).
map
(
rootFolder:
:
relativize
);
}
}
src/main/resources/application-local.properties
View file @
b5c87a67
...
...
@@ -22,5 +22,5 @@ spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
#establecer el tamao maximo de un archivo
spring.servlet.multipart.max-file-size
=
5MB
#establecer el tamao maximo de varios archivos enviados a la vez de una misma solicitud
spring.servlet.multipart.max-request-size
=
10
MB
media.location
=
mediafiles
spring.servlet.multipart.max-request-size
=
5
MB
#
media.location=mediafiles
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