-1
        String uri = "URL";
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());    
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
        HttpEntity<String> entity = new HttpEntity<String>(headers);

        ResponseEntity<byte[]> response = restTemplate.exchange(uri, HttpMethod.GET, entity, byte[].class, "1");

I get response like-> <200,[B@c505096,[Pragma:"no-cache", Content-Disposition:"inline; filename=935436242330664960_pratikpopo.txt", Expires:"0", Cache-Control:"no-cache, no-store, must-revalidate", Content-Type:"application/octet-stream", Content-Length:"18", Date:"Mon, 31 Jan 2022 04:16:56 GMT"]> I want to downlod (935436242330664960_pratikpopo.txt) this file. Is there any way to download this file

  • See if this helps - https://stackoverflow.com/a/26615303/17981941 – Dhanraj Jan 29 '22 at 04:55
  • Welcome to StackOverflow. Would you please give some details as to what is not working? – rajah9 Jan 30 '22 at 13:54
  • <200,[B@c505096,[Pragma:"no-cache", Content-Disposition:"inline; filename=935436242330664960_pratikpopo.txt", Expires:"0", Cache-Control:"no-cache, no-store, must-revalidate", Content-Type:"application/octet-stream", Content-Length:"18", Date:"Mon, 31 Jan 2022 04:16:56 GMT"]> I am getting this response but i am not able to download pratikpopo.txt file download. My motive is store this file in local machine how should we do – pratikpoponimb Jan 31 '22 at 04:19

2 Answers2

0
  @RequestMapping(value = "/download3/{path}", method = RequestMethod.GET)
    public String downloadFile4( @RequestParam("file_name") String path) throws IOException
    {
        String uri = "File Path-URL(location wehere your file is stored on server)";
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());    
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
        HttpEntity<String> entity = new HttpEntity<String>(headers);

        ResponseEntity<byte[]> response = restTemplate.exchange(uri, HttpMethod.GET, entity, byte[].class, "1");
        
        URL furl = new URL(uri);
        ReadableByteChannel rbc = Channels.newChannel(furl.openStream());
        FileOutputStream fos = new FileOutputStream("C:/download-file/"+ response.getHeaders().getContentDisposition().getFilename());
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        
        return "file downloaded";
    }
0
{
        URL link = new URL("your full url http://........");
        InputStream in = new BufferedInputStream(link.openStream());
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int n = 0;
        while (-1!=(n=in.read(buf)))
        {
           out.write(buf, 0, n);
        }
        out.close();
        in.close();
        byte[] response = out.toByteArray();
        
        FileOutputStream fos = new FileOutputStream("C:/download-file/YourFileName.txt");
        fos.write(response);
        fos.close();
 }
Saman Salehi
  • 1,004
  • 1
  • 12
  • 19