Question

I have two applications (one in Python and another in Java). They are connected via XML-RPC protocol. I have image files saved in database as binary data and i need to get them in Java.

So i make a query to get the data in python and i return a dictionary as you can see below:

            aux = {
                    'location_id' : location['id'],
                    'name' : location['name'] or " ",
                    'units' : location['units'] or 0.0,
                    'area' : location['area'] or 0.0,
                    'day_meter' : location['day_meter'] or 0.0,
                    'free' : location['free'] or 0.0,    
                    'type' : location['type'] or " ",
                    'loc_type' : location['loc_type'] or " ",
                    'image' : str(location['image']),                                                                    
               }

I receive the data without problems in Java but i can not get the image file from the binary data. Here is my code:

I make a cast to convert the binaty data to a byte [] in Java

        byte[] b = aux.get("image").toString().getBytes("UTF-8");
        l.setByteImg(b);

And here i try to construct a BufferedImage.

        InputStream in = new ByteArrayInputStream(l.getByteImg());
        BufferedImage bImageFromConvert = ImageIO.read(in);

But i get a null in my bImageFromConvert.

What am i doing wrong??

Was it helpful?

Solution

Xml does not support embbeding binary data directly, you need to convert it to a text string. Usually base64 is used to do this.
Try to encode image data in python and convert it back to binary stream in java.

python:

aux = {
                    'location_id' : location['id'],
                    'name' : location['name'] or " ",
                    'units' : location['units'] or 0.0,
                    'area' : location['area'] or 0.0,
                    'day_meter' : location['day_meter'] or 0.0,
                    'free' : location['free'] or 0.0,    
                    'type' : location['type'] or " ",
                    'loc_type' : location['loc_type'] or " ",
                    'image' : base64.b64encode(str(location['image'])),                                                                    
               }

java

byte[] image = javax.xml.bind.DatatypeConverter.parseBase64Binary(aux.get("image").toString());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top