2

I am trying to send a blob data from oracle db to objective c via webservices. The blob data is a word document stored in oracle db.

Following is what I did so far:

  1. Obtain the blob object through java jdbc
  2. Convert the blob object into java byte array
  3. Setting this byte array in a plain java object (serialized) and sending it to the web service
  4. Webservice sends the plain java object to objective-c with Media-type as application/json
  5. The json object is retrieved in the objective-c and the byte array is assigned to NSData
  6. NSData is then written to a file and the file is opened

Problem: I am unable to open the file through objective-c and when I open the file physically in the mac it contains some junk characters. Can anyone please let me know if there is anyway that a java byte array which represents a word document could be converted to a objective-c recognized data type and finally open the word document in the objective-c app.

Following is the code for your perusal on the objective-c end:

Note: "sourceDoc" in the code is the key for byte array.

NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

if ([data length] >0  && error == nil){

    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    NSData *returnData = nil;

    for(NSDictionary *billDict in jsonArray){
        returnData = [NSData dataWithBytes:(__bridge const void *)([billDict objectForKey:@"sourceDoc"]) length:[[billDict objectForKey:@"sourceDoc"] length]];
        break;
    }
}
Joe
  • 56,979
  • 9
  • 128
  • 135
Ashok Ambrose
  • 191
  • 3
  • 17

1 Answers1

0

The problem is that -[NSData dataWithBytes:length:] expects a pointer to the actual data but you are passing it an object from the dictionary. You should inspect the contents returned using NSLog(@"%@", [billDict objectForKey:@"sourceDoc"]); but I expect it will be a base64 string. If it is a base64 string you can use one the base64 decoding options from this question: How do I do base64 encoding on iphone-sdk?. If you use Alex Reynolds answer from that question then you would include the base64 decoding file header you created and set returnData in your above code like this:

returnData = [NSData base64DataFromString:[billDict objectForKey:@"sourceDoc"]];
Community
  • 1
  • 1
Joe
  • 56,979
  • 9
  • 128
  • 135
  • I imported the base64library and did the same mentioned above. I got a word document with following characters in it z`n6„USªÆæãÛN=QEzæ˛=ıúI]|˜k`ÛœæÛœ<Ûœ<Ûœ<Ùûù´^ök¢ö‚jÿ®û¿®öh≠µÁÖä«ï – Ashok Ambrose Dec 19 '12 at 19:19
  • Did you inspect the contents of the string `billDict` returns for `sourceDoc` yet to make sure it was valid base64? – Joe Dec 19 '12 at 20:40
  • Yes I printed it and this is what I get - ÐÏà¡±á – Ashok Ambrose Dec 19 '12 at 22:30
  • Hi Joe, This approach you proposed is working now. The mistake which I did was, I converted the byte array to a string(in java) instead of sending the raw byte array to objective-c. Thanks a lot. – Ashok Ambrose Dec 19 '12 at 23:24
  • You are welcome I was getting ready to reply that that was your issue. – Joe Dec 19 '12 at 23:27