How to download a file via rest

How to download a file via rest

how to download a file via rest

stackoverflow.com › questions › download-file-with-rest-api. We're using HTTP Request to call the REST API. The REST API, POST, has a response with an attachment file. How can we download this file. Download a Shared File (REST API: GET). This operation downloads a backed up file that was shared with you by another user. Note: Downloading a folder is. how to download a file via rest

czetsuya tech

Normally I create an interface when defining a REST api, this is useful when testing using Arquillian. To define a download file api we must define the method below in the interface. @GET @Path("/downloadFile") ActionStatus downloadFile(@QueryParam("file") String file);
And then in the implementation we have: public void downloadFile(String filePath) throws BusinessApiException { HttpServletResponse response = // see http://czetsuya-tech.blogspot.com/2017/09/how-to-access-httpservletrequest-and.html File file = new File(getProviderRootDir() + File.separator + filePath); if (!file.exists()) { // handle exception } try { FileInputStream fis = new FileInputStream(file); response.setContentType(Files.probeContentType(file.toPath())); response.setContentLength((int) file.length()); response.addHeader("Content-disposition", "attachment;filename=\"" + file.getName() + "\""); IOUtils.copy(fis, response.getOutputStream()); response.flushBuffer(); } catch (IOException e) { // handle exception } }
*Note: IOUtils is from apache commons.
Источник: [https://torrent-igruha.org/3551-portal.html]

How to download a file via rest

3 thoughts to “How to download a file via rest”

Leave a Reply

Your email address will not be published. Required fields are marked *