Announcement Announcement Module
Collapse
No announcement yet.
Opening SAAJ Attachment in Browser Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Opening SAAJ Attachment in Browser

    Hi

    I'm getting ecxel attachment via SAAJ. I've to get excel attachment and open the same in browser. I did server side work. I've no idea what to do after I get the attachment? Do I need to create a tmp file? can I write data to response object directly?

    Could you please suggest me how to do this?

    Thanks
    Ramesh

  • #2
    You can get an attachment by calling getAttachment() on the SoapMessage, and after that you can get an inputstream of it, which you can stream to the HttpServletResponse outputstream, by using FileCopyUtils in Spring Core (for instance).

    Comment


    • #3
      thanks for your reply.
      I was able to get the attachment and input stream too. I don't have an idea after this. My aim is to open this in JSP page. How can I send this input stream to JSP?

      Could you please tell how to do using Struts?

      Thanks
      Ramesh

      Comment


      • #4
        Basically, you don't want to use JSP for this, just direct HTTP request handling. So make sure the controller or action that downloads the attachment is mapped to some URL, and in that controller use FileCopyUtils to copy the inputstream to the HttpServletResponse.getOutputStream() in the controller or action.

        Comment


        • #5
          HI Arjen

          Thanks for your reply and your solution.
          I'm using struts framework and I've the following code in Action class to download ( excel sheet) the attachment

          int numOfAttachments = rp.countAttachments();
          Iterator attachment = rp.getAttachments();
          while (attachment.hasNext()) {
          attachPart = (AttachmentPart)attachment.next();
          }

          InputStram is = attachPart.getDataHandler().getInputStream();

          Can you tell me how can I open this attachement (excel sheet) in browser?
          is there any alternatice for copyUtils for struts?

          Thanks
          Ramesh

          Comment


          • #6
            In the execute method of your action:
            Code:
            public ActionForward execute(ActionMapping mapping,
                                         ActionForm form,
                                         javax.servlet.http.HttpServletRequest request,
                                         javax.servlet.http.HttpServletResponse response)
                                  throws java.lang.Exception
            do this:
            Code:
            FileCopyUtils.copy(is, response.getOutputstream());
            return null;

            Comment


            • #7
              HI
              Thanks a lot.
              Since I'm using I used the following code in action class. SAAJ Attachment (excel sheet) got opened in browser.

              code:
              --------

              response.setContentType("application/vnd.ms-excel");
              ServletOutputStream sosStream = null;
              try
              {
              sosStream = response.getOutputStream();
              int ibit = 256;
              while ((ibit) >= 0)
              {
              ibit = inStream.read();
              sosStream.write(ibit);
              }

              catch (IOException ioeException)
              {
              }

              Thanks
              Ramesh

              Comment


              • #8
                Yeah, except that the

                Code:
                try
                {
                sosStream = response.getOutputStream();
                int ibit = 256;
                while ((ibit) >= 0)
                {
                ibit = inStream.read();
                sosStream.write(ibit);
                }
                
                catch (IOException ioeException)
                {
                }
                can be replaced with the one-liner using FileCopyUtils. Does exactly the same, or even more (like proper error handling and closing of the streams

                Comment

                Working...
                X