public String saveEmbededResponse(
      String downloadDirectory,
      String localFileName,
      com.actuate.schemas.DownloadFileResponse downloadFileResponse,
      boolean decomposed)
      throws IOException, RemoteException {

    String downloadName = null;
    BufferedOutputStream outStream = null;
    String localFilePath = downloadDirectory + "/" + localFileName;

    try {
      if (!decomposed) {
        outStream = new BufferedOutputStream(new FileOutputStream(localFilePath));
      }

      com.actuate.schemas.Attachment attachment = downloadFileResponse.getContent();
      if (attachment != null) {
        byte[] b = attachment.getContentData();
        System.out.println("Attachment retrived as " + attachment.getContentId());
        if (b != null && outStream != null) {
          outStream.write(b);
        }
      }

      com.actuate.schemas.ArrayOfAttachment arrayOfAttachment =
          downloadFileResponse.getContainedFiles();

      if (arrayOfAttachment != null) {
        com.actuate.schemas.Attachment[] attachments = arrayOfAttachment.getAttachment();
        for (int i = 0; i < attachments.length; i++) {
          if (attachments[i] != null) {
            byte[] b = attachments[i].getContentData();
            System.out.println("Attachment retrived as " + attachments[i].getContentId());
            if (b != null) {
              if (outStream != null) {
                outStream.write(b);
              } else {
                String decomposedDocAttachment =
                    downloadDirectory + "/" + attachments[i].getContentId();
                BufferedOutputStream tempOutStream =
                    new BufferedOutputStream(new FileOutputStream(decomposedDocAttachment));
                tempOutStream.write(b);
                tempOutStream.close();
              }
            }
          }
        }
      }

    } catch (RemoteException e) {
      throw e;
    } finally {
      if (outStream != null) {
        downloadName = localFilePath;
        outStream.close();
      }
    }
    return downloadName;
  }
  /**
   * Download file from encyclopdia to the specified directory If document is decomposed ,multiple
   * attachments of files will be discarded as its not in viewable format.But attachments ids will
   * be shown to user.This example can be modified easily to save those attachments as different
   * files.
   *
   * @param FileName
   * @param decomposeCompoundDocument
   * @param downloadEmbedded
   * @param downloadDirectory
   * @return boolean
   */
  public String downloadFile(
      String FileName,
      boolean decomposeCompoundDocument,
      boolean downloadEmbedded,
      String downloadDirectory)
      throws Exception {
    System.out.println("Download " + FileName);
    com.actuate.schemas.DownloadFile downloadFile = new com.actuate.schemas.DownloadFile();
    downloadFile.setFileName(FileName);
    downloadFile.setDecomposeCompoundDocument(new Boolean(decomposeCompoundDocument));
    downloadFile.setDownloadEmbedded(new Boolean(downloadEmbedded));

    String downloadName = null;
    com.actuate.schemas.DownloadFileResponse downloadFileResponse = null;
    try {
      downloadFileResponse = proxy.downloadFile(downloadFile);

      String serverFilePath = downloadFileResponse.getFile().getName();
      String localFileName =
          serverFilePath.substring(serverFilePath.lastIndexOf('/') + 1, serverFilePath.length());

      if (!downloadEmbedded) {
        downloadName =
            saveNonEmbededResponse(downloadDirectory, localFileName, decomposeCompoundDocument);
      } else {
        downloadName =
            saveEmbededResponse(
                downloadDirectory, localFileName, downloadFileResponse, decomposeCompoundDocument);
      }

    } catch (SOAPException e) {
      throw AxisFault.makeFault(e);
    } catch (RemoteException e) {
      throw e;
    } catch (IOException e) {
      throw e;
    }
    return downloadName;
  }