no

How to download a file in SOAP API

First we define the interface like this: @WebMethod ActionStatus downloadFile(@WebParam(name = "file") String file); Implement...

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.

Related

javaee-rest 6992420043200775415

Post a Comment Default Comments

item