'Download server file in browser | Download file on browser
@RequestMapping(value = "/download1", method = RequestMethod.GET)
public ResponseEntity<Object> downloadFile() throws IOException
{
String filename = "http://k8s-di-..../doc/getfile/path/4960_popo.txt";
// String filename = "C:/download-file/pk.txt";
File file = new File(filename); //line3......
InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition",
String.format("attachment; filename=\"%s\"", file.getName()));
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
ResponseEntity<Object> responseEntity = ResponseEntity.ok().headers(headers)
.contentLength(file.length())
.contentType(MediaType.parseMediaType("application/txt")).body(resource);
return responseEntity;
}
when i use local file i.e("C:/download-file/pk.txt") code will work fine file will download on browser But When i use server file i.t will not work.When i use this url("http://k8s-di-..../doc/getfile/path/4960_popo.txt") line no.3 in file value will show http:\k8s-didi....\doc\getfile\path\4960_popo.txt. This url is correct? Error - (The filename, directory name, or volume label syntax is incorrect) Is there any suggestion please give me. My motive is download file in browser.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
