After a good night's sleep looking for how to solve my concern, the answer was much simpler than expected and says:
The physical path where you save the files and the URL with which you want to download is different . Here is a summary of what was done
Through the ServletContext injected in the request I get the RealPath ("/") of the application. From there the different folders where you want to save the files will be concatenated eg: String rutaFisica = this.context.getRealPath("/") + "/recursos/archivos/"
if the directory does not exist must be created.
In my case the files will be stored in the path C:\appsvr\jboss-eap-7.1\standalone\deployments\NombreProyecto.war\recursos\archivos\
Now to be able to download it we create a web service where the Path
will make up the url to access the file
@Path("/descargas")
public class DescargaService {
@Path("/archivos/{nombreArchivo}")
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response descargarArchivo(@Context ServletContext context, @PathParam("nombreArchivo") String archivo) {
String pathfile = context.getRealPath("/") + "/recursos/archivos/" + archivo;
File file = new File(pathfile);
if(file.exists()) {
ResponseBuilder response = Response.ok((Object) file);
response.header("Content-Disposition", "attachment;filename=" + archivo);
return response.build();
}else {
return Response.status(Status.NOT_FOUND).build();
}
}
}
In this way the url to download the file would be http://host:port/contextroot/url-pattern/descargas/archivos/nombrearchivo
, it is to clarify that the contextroot
is the majority of cases is the name of the application and the url-pattern
is the configuration of servletMapping
of web.config