开发者

Send byte array from web service to client

I want to send a byte array from a web service to a client that requests an operation exposed via the service. In my method, I read an image into a byte array. I think place this byte array into a wrapper POJO. This is the return type for the operation.

@Override
public ImageWrapper getImage() {
    File imageFile = new File("C:\\images\\car.jpg");
    ImageWrapper wrapper = null;
    try {
        BufferedImage img = ImageIO.read(imageFile);
        ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
        ImageIO.wri开发者_Python百科te(img, "jpg", baos);
        baos.flush();
        byte[] result = baos.toByteArray();
        baos.close();
        wrapper = new ImageWrapper();
        wrapper.setContent(result);
        System.out.println("Service image wrapper: " + wrapper);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return wrapper;
}

I can receive the ImageWrapper object in the client ok. It has a different id to the ImageWrapper instance that is created by the web service on the server, as I would expect. But, the problem is that when I try to get the byte[] array from the ImageWrapper, it is null... Any ideas why? The wrapper class looks like:

package soap.service.model;

public class ImageWrapper {
    private byte[] content;

    public void setContent(byte[] content) {
        this.content = content;
    }

    public byte[] getImg() {
        return this.content;
    }
}

and the client looks like:

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import soap.service.model.ImageWrapper;
import soap.service.sei.ImageSei;

public class ImageClient {
    public static void main(String... args) throws MalformedURLException {
        URL url = new URL("http://localhost:8080/image?wsdl");
        QName qname = new QName("http://impl.service.soap/", "ImageImplService");
        Service service = Service.create(url, qname);
        ImageSei sei = service.getPort(ImageSei.class);
        ImageWrapper iw = sei.getImage();// This is ok
        System.out.println(iw.getImg()); // * This is null
    }
}

========================================================================

Update Even if I change the byte array in ImageWrapper to a String, it still comes back as 'null' in the client. I have my web service set to use 'Document' style also.


Your interface object (the one getting serialized and being transfered) does not contain public data (only a method to get private data). Your byte[] should be a public field or property to be included in the serialized data

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜