Where to host files in maven web project

0

I have a web project built with maven and eclipse that from a byte array I must write the files inside the project folders and then through a url the files can be downloaded. Looking for the files I could leave in the resources folder. Indeed, if I place the file there I can download it with the url http://host:port/contextroot/nombre_archivo.ext but I want the download path to be http://host:port/contextroot/carpeta1/carpeta2/carpeta3/nombre_archivo.ext , for that it creates within the project in the folder resources that structure but when I look for the file in the second url not found appears and if you show it to me in the first url.

The files should be placed in that folder or how can I download them with the second url.

Thank you very much.

    
asked by isaac 17.11.2018 в 06:38
source

1 answer

0

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

        
    answered by 19.11.2018 в 05:15