cancel
Showing results for 
Search instead for 
Did you mean: 

help - regarding byte[ ]

Former Member
0 Kudos

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

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

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

Former Member
0 Kudos

Hi Narendra,

I am not using any fileupload UI, i have already get the file in byte[] form

Former Member
0 Kudos

solved by my own using inputStream

Former Member
0 Kudos

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

Former Member
0 Kudos

hi, waht shall i put for this

InputStream is = new FileInputStream("Pass here the resource path");

?

pass the resource path? pls advise. thanks