Results 1 to 8 of 8

Thread: Opening SAAJ Attachment in Browser

  1. #1
    Join Date
    Mar 2007
    Posts
    19

    Default 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. #2
    Join Date
    Jul 2005
    Location
    Rotterdam, the Netherlands
    Posts
    1,562

    Default

    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).
    Arjen Poutsma

    Spring Web Services Dev Lead
    Please read the FAQ

  3. #3
    Join Date
    Mar 2007
    Posts
    19

    Default

    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

  4. #4
    Join Date
    Jul 2005
    Location
    Rotterdam, the Netherlands
    Posts
    1,562

    Default

    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.
    Arjen Poutsma

    Spring Web Services Dev Lead
    Please read the FAQ

  5. #5
    Join Date
    Mar 2007
    Posts
    19

    Default

    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

  6. #6
    Join Date
    Jul 2005
    Location
    Rotterdam, the Netherlands
    Posts
    1,562

    Default

    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;
    Arjen Poutsma

    Spring Web Services Dev Lead
    Please read the FAQ

  7. #7
    Join Date
    Mar 2007
    Posts
    19

    Default

    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

  8. #8
    Join Date
    Jul 2005
    Location
    Rotterdam, the Netherlands
    Posts
    1,562

    Default

    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
    Arjen Poutsma

    Spring Web Services Dev Lead
    Please read the FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •