Browse Source

Return file downloads as byte arrays in controller

Updated downloadDocument and downloadReport endpoints to return ResponseEntity<byte[]> instead of FileSystemResource. This change ensures files are sent as byte arrays, improving compatibility with clients expecting raw file data.
Mario Martínez Hernández 3 weeks ago
parent
commit
031b6c30bf
1 changed files with 11 additions and 5 deletions
  1. 11 5
      src/main/java/es/uv/saic/web/DownloadController.java

+ 11 - 5
src/main/java/es/uv/saic/web/DownloadController.java

@@ -129,7 +129,7 @@ public class DownloadController {
 	 */
 	@GetMapping(value="/download/document/{id}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
 	@ResponseBody
-	public ResponseEntity<FileSystemResource> downloadDocument(@PathVariable("id") Integer idDocument, HttpServletResponse response) throws FileNotFoundException {		
+	public ResponseEntity<byte[]> downloadDocument(@PathVariable("id") Integer idDocument, HttpServletResponse response) throws FileNotFoundException {		
 		Document document = ds.findById(idDocument);
 		FileSystemResource file = new FileSystemResource(document.getRuta());
 		if(!file.exists()) {
@@ -137,7 +137,13 @@ public class DownloadController {
 		}
 		
 		response.setHeader("Content-Disposition", "attachment; filename="+file.getFilename());
-		return ResponseEntity.ok(file);
+		try {
+			return ResponseEntity.ok(file.getInputStream().readAllBytes());
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+		return null;
 	}
 
 	/*
@@ -150,7 +156,7 @@ public class DownloadController {
 	 */
 	@GetMapping(value="/download/report/{t}/{p}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
 	@ResponseBody 
-	public ResponseEntity<FileSystemResource> downloadReport(@PathVariable("t") Integer idTitulacio, @PathVariable("p") String nomProces,
+	public ResponseEntity<byte[]> downloadReport(@PathVariable("t") Integer idTitulacio, @PathVariable("p") String nomProces,
 			HttpServletResponse response) throws IOException, XDocReportException {
 		
 		OrganTransferDTO titulacio = core.findOrganById("T", idTitulacio);
@@ -163,11 +169,11 @@ public class DownloadController {
 		if(it != null) {
 			if((new File(this.filePath+it.getEvidencia())).exists()) {
 				response.setHeader("Content-Disposition", "attachment; filename="+Integer.toString(idTitulacio)+"_"+nomProces+".pdf");
-				return ResponseEntity.ok(new FileSystemResource(this.filePath+it.getEvidencia()));
+				return ResponseEntity.ok(new FileSystemResource(this.filePath+it.getEvidencia()).getInputStream().readAllBytes());
 			}
 		}
 		
-		return ResponseEntity.ok(new FileSystemResource(this.filePath+it.getEvidencia()));
+		return ResponseEntity.ok(new FileSystemResource(this.filePath+it.getEvidencia()).getInputStream().readAllBytes());
 	}