download filename

This commit is contained in:
manalejandro 2018-07-15 04:30:38 +02:00
parent 236e3b4047
commit 93f951ca22

View File

@ -3,6 +3,7 @@ package com.manalejandro.arjion.controllers;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URLEncoder;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -130,7 +131,7 @@ public class MainController {
ByteArrayResource resource = new ByteArrayResource(Files.readAllBytes(path)); ByteArrayResource resource = new ByteArrayResource(Files.readAllBytes(path));
String type = file.toURL().openConnection().guessContentTypeFromName(filename); String type = file.toURL().openConnection().guessContentTypeFromName(filename);
HttpHeaders responseHeaders = new HttpHeaders(); HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.add("content-disposition", "attachment; filename=" + filename); responseHeaders.add("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
responseHeaders.add("Content-Type", type); responseHeaders.add("Content-Type", type);
return ResponseEntity.ok().contentLength(file.length()).headers(responseHeaders) return ResponseEntity.ok().contentLength(file.length()).headers(responseHeaders)
.contentType(MediaType.parseMediaType("application/octet-stream")).body(resource); .contentType(MediaType.parseMediaType("application/octet-stream")).body(resource);