How to download a file in SOAP API
Tech-Today

How to download a file in SOAP API


First we define the interface like this:

@WebMethod
ActionStatus downloadFile(@WebParam(name = "file") String file);

Implementation:

public ActionStatus downloadFile(String filePath) {
MessageContext mc = webServiceContext.getMessageContext(); // see http://czetsuya-tech.blogspot.com/2017/09/how-to-access-httpservletrequest-and.html
HttpServletResponse response = (HttpServletResponse) mc.get(MessageContext.SERVLET_RESPONSE);

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.




- How To Download A File In Angular2
There are 2 ways I use to download a file with Angular2 or greater. For this example, we will use a Java REST service. The first approach would be taking advantage of the HTTP download, where the Angular side will just call a URL that will initiate the...

- Display A Byte[] Image From A Rest Web Service
This tutorial will not delve into angular nor will we discuss what a directive is. We will not try to explain what a rest web service is. Instead we will provide a demonstration via actual code, how we can consume a rest service from angular that returns...

- How To Upload A File In Rest Api
This is how I define the method: @POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) ActionStatus uploadFile(@MultipartForm FileUploadForm form); And then the implementation: public ActionStatus uploadFile(FileUploadForm form) { File file...

- How To Access Httpservletrequest And Httpservletresponse In Both Rest And Soap Service
There are times when we need either HttpServletRequest or HttpServletResponse in our REST or SOAP api. These objects are readily available if you know how to inject them. Common usage of HttpServletRequest is when getting the request variables...

- How To Download A File From The Server Using Javaee
The following snippet will accept a filename in the server's directory, set it as the content of the FacesContext for the user to download. public String downloadXMLInvoice(String fileName) { File file = new File(getXmlInvoiceDir().getAbsolutePath()...



Tech-Today








.