Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Saving WebDav resource into ByteArray or InputStream

Posted on 2004-09-01
27
Medium Priority
?
406 Views
Last Modified: 2008-02-01

Hello all

this the process to save the WebDAV resource to the file
  webDavResourceAttachmentFile.getMethod(new File(strAttachmentFile));


But I wanted to have this in other way.

It should save to either ByteArray or InputStream

How can I do this??

I um using Apache HttpClient and Slide API for this

thanks
Sudhakar
0
Comment
Question by:sudhakar_koundinya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 11
  • 3
27 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11950752
Read it into a ByteArrayInputStream
0
 
LVL 92

Expert Comment

by:objects
ID: 11950756
what class is webDavResourceAttachmentFile

> It should save to either ByteArray or InputStream

that'll be an output stream wouldn't it :)
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11950760
yes, But how??
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11950769
webDavResourceAttachmentFile is of type org.apache.webdav.lib.WebdavResource
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11950774
What is the f-q name of the thing you're reading from?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11950786
>>that'll be an output stream wouldn't it :)

Yes ;-)

while ((bytesRead = in.read(buffer)) > -1) {
    baos.write(buffer, 0, bytesRead);
}
byte[] resourceAsBytes = baos.toByteArray();
// close everything
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11950790
>> f-q name??

I didn't get that

basically above code snippet downloads the WebDavResoure from MSExchange Server and saves it into file
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11950806
>>Yes ;-)

that is not OutputStream object my friend. That is WebDavResource
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11950807
(f-q == fully qualified)

Can't find JavaDoc for that f-q name
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11950816
The code i possted above should work fine. 'in' is simply an InputStream opened on the resource
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11950824
this is online doc for WebDavClient API

http://jakarta.apache.org/slide/clientjavadoc/index.html
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11950843
>> The code i possted above should work fine. 'in' is simply an InputStream opened on the resource

Until and unless API supports that
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11950851
byte[] resourceAsBytes = wdr.getMethodDataAsString().getBytes();
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11950855
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 11950862
...and if the resource is not character data:

InputStream in = getMethodData();
// etc as above
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11950864
wdr.getMethodDataAsString().getBytes(); will not work for binary files :(
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11950866
>>but always returning 0 bytes

Then the path to the data may be wrong
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11950879
>>Then the path to the data may be wrong

No if that is the case my first snippet also should not work. But it is working
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11950901
Are you calling your code first? - perhaps you need to
0
 
LVL 92

Expert Comment

by:objects
ID: 11950914
Have a look at the source code, it could give you some clues as to what its doing.
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11950920
>> Are you calling your code first? - perhaps you need to

I didn't get that. But I am doing everything in sequential order only

I just found one more code. I will check that and let u know

            org.apache.commons.httpclient.methods.UrlGetMethod getMethod= new                          org.apache.commons.httpclient.methods.UrlGetMethod(uri);
            int status=client.executeMethod(getMethod);
            System.out.println(status);
            byte is[]=getMethod.getResponseBody();
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11950940
This is my modified method

  protected void saveAttachment(String folder, String filename,
                                String strAttachmentName,
                                String mailbox) throws
      MSExchangeServerException {

    try {

      strAttachmentName = URLDecoder.decode(strAttachmentName);
      org.apache.commons.httpclient.HttpURL urlEML = (org.apache.commons.
          httpclient.HttpURL) getURL(path + "/" +
                                     mailbox + "/" + folder + "/" + filename +
                                     "/" + strAttachmentName +
                                     "/");

      if (urlEML == null) {
        return;
      }

      /*org.prithvi.docparser.microsoft.msexchange.apache.OwaWebdavResource
          webDavResourceAttachmentFile =
          getWebResource(urlEML);

      if (webDavResourceAttachmentFile == null) {
        return;
      }*/

        String uri= path + "/" + mailbox + "/" + folder + "/" + replaceSpecial(filename, "\":?*<>\\| ")+"/" + replaceSpecial( strAttachmentName , "\":?*<>\\| ");
        //String uri= path + "/" + mailbox + "/" + folder + "/" + filename +"/" + strAttachmentName ;
        System.out.println(uri);

        

            org.apache.commons.httpclient.methods.UrlGetMethod getMethod= new org.apache.commons.httpclient.methods.UrlGetMethod(uri);
            int status=client.executeMethod(getMethod);
            System.out.println(status);
            //byte is[]=getMethod.getResponseBody();

      //get the attachment
      String strAttachmentFile = targetPath + "/" + mailbox + "/" + folder +
          "/" + filename.replaceAll(":", "%3A") + "_Dir/" + strAttachmentName;

      if (targetPath != null) {
        //webDavResourceAttachmentFile.getMethod(new File(strAttachmentFile));
            //InputStream is= webDavResourceAttachmentFile.getMethodData();
            //String str=webDavResourceAttachmentFile.getMethodDataAsString();
            //System.out.println(str);
      }
        /*if (targetPath != null)
        {
            FileOutputStream mailStream = new FileOutputStream(strAttachmentFile);
            mailStream.write(is);
            mailStream.close();
            mailStream = null;
        }*/
       
      

    }
    catch (Exception ex) {

      throw new MSExchangeServerException(
          "Unknown Exception raised while saving the attachment", ex);
    }
  }
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11950952
I will let u know if that works  :-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11950955
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 11951010
yes I was looking at that earlier, but thought you needed to use the instance you already had.
you can also trry looking at the getResponseBodyAsStream() method
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11957498
Although, I have solved my problem for closing the question I splitted the points

Thanks
Sudhakar
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11968955
:-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question