Skip to Content
author's profile photo Former Member
Former Member

help - regarding byte[ ]

Dear all,

i have this

byte[] att_content = new byte[255];

att_content = attach[0].getResource();

i have this..attach[0].getResource() which will return me the contents of the attachment attached to the adobe form..cut it short..it is of byte[] type..so i declared a byte[] att_content to hold it..

after i get this att_content,i need to send it back to my SAP system and create the attahcment to a PR...i get to do the attahcment, but when i try to open the attached file,(a MS WORD doc).the wording in the MS word all become unreadable...i think is the problem of the byte[] i send in.....

any idea how to get ti fix? thank you

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Jan 16, 2009 at 04:32 AM

    Hi Joan,

    I think you cannot directly pass the file to byte array. and thats why you are getting the unreadable format.

    Please use the below method which returns byte array.

    // Returns the contents of the file in a byte array.
        public static byte[] getBytesFromFile(File file) throws IOException {
            InputStream is = new FileInputStream("Pass here the resource path");
        
            // Get the size of the file
            long length = file.length();
        
            // You cannot create an array using a long type.
            // It needs to be an int type.
            // Before converting to an int type, check
            // to ensure that file is not larger than Integer.MAX_VALUE.
            if (length > Integer.MAX_VALUE) {
                // File is too large
            }
        
            // Create the byte array to hold the data
            byte[] bytes = new byte[(int)length];
        
            // Read in the bytes
            int offset = 0;
            int numRead = 0;
            while (offset < bytes.length
                   && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
                offset += numRead;
            }
        
            // Ensure all the bytes have been read in
            if (offset < bytes.length) {
                throw new IOException("Could not completely read file "+file.getName());
            }
        
            // Close the input stream and return bytes
            is.close();
            return bytes;
        }

    Regards

    Narendra

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 16, 2009 at 05:09 AM

    Hi,

     File file = new File("C:\abc.doc")// pass the full path here and then use file in the below code
    public static byte[] getBytesFromFile(File file) throws IOException {
            InputStream is = new FileInputStream(file);

    I

    Regards

    Narendra

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.