Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

253
Views
descargar archivo java spring rest api

quiero hacer un controlador api de descanso (arranque de primavera) que cuando se solicite con un get me permita descargar un archivo de Excel. actualmente tengo este punto final:

 @RequestMapping(value = "/download.xls", method = RequestMethod.GET) public ResponseEntity Survey_Reports(@RequestParam(value = "evaluated") String evaluated){ return surveyService.getSurveysFile(evaluated); }

que en última instancia llama a este método:

 public static ResponseEntity getDownloadResponse() { File file2Upload = new File("Survey_Reports.xls"); Path path = Paths.get(file2Upload.getAbsolutePath()); ByteArrayResource resource = null; try { resource = new ByteArrayResource(Files.readAllBytes(path)); } catch (IOException e) { logger.error("there was an error getting the file bytes ", e); } return ResponseEntity.ok() .contentLength(file2Upload.length()) //this line doesnt seem to work as i set the file format in the controller request mapping .contentType(MediaType.parseMediaType("application/vnd.ms-excel")) .body(resource); }

todo parece funcionar semi-bien cuando obtengo el archivo download.xls (como el mapeo) correctamente, pero ahora quiero hacer que el archivo descargado tenga un nombre específico como: namedName.xls o userDateEndDate.xls o alguna otra cosa, es ¿Hay alguna manera de editar la entidad de respuesta para hacerlo? para que no tenga que nombrar el mapeo "download.xls"

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

En el contexto de la respuesta HttpServletResponse , puede hacer esto así

 response.setContentType("application/csv"); response.setHeader("Content-Disposition", "attachment; filename=" + csvName);

y para ResponseEntity supongo que puedes usar algo como esto:

 ResponseEntity.ok().header("Content-Disposition","attachment; filename=" + csvName );
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!