Hi All,
I wanted to send file across webservices using weblogic 10.I used stream attachments and Datahandler to send the file. But in my client program when i try to retrieve the data as bytes using Datahandler, the number of bytes i receive is zero. Can anyone help me why this happens?
_*Webservices code:*_
import javax.jws.WebMethod;
import javax.jws.WebService;
import weblogic.jws.WLHttpTransport;
import weblogic.jws.StreamAttachments;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
@WebService(name="StreamAttachPortType",
serviceName="StreamAttachService",
targetNamespace="_http://example.org")
@WLHttpTransport(contextPath="stream_attach",
serviceUri="StreamAttachService",
portName="StreamAttachServicePort")
@StreamAttachments
public class FileRetrieverService {
@WebMethod()
public DataHandler getFileAsAttachment(String filename)
{
return(new DataHandler(new FileDataSource(filename)));
}
}
Client code:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.rmi.RemoteException;
import java.util.Date;
import javax.activation.DataHandler;
import javax.xml.rpc.ServiceException;
public class FileRetrieverClient {
public static void main(String[] args) throws ServiceException, RemoteException, IOException {
StreamAttachService service = new StreamAttachService_Impl(args[0]);
StreamAttachPortType port = service.getStreamAttachServicePort();
DataHandler dh = port.getFileAsAttachment("c:\\test.txt");
System.out.println(dh.getContentType());
FileOutputStream fos = new FileOutputStream("c:\\test-output.txt");
fos.write(getBytesFromStream(dh.getInputStream()));
fos.close();
}
private static byte[] getBytesFromStream(InputStream is) throws IOException {
byte[] bytes = new byte[is.available()];
int offset = 0;
int numRead = 0;
while ( (offset < bytes.length)
&&
( (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) ) {
offset += numRead;
}
if (offset < bytes.length) {
throw new IOException("Could not completely read from stream");
}
is.close();
return bytes;
}
}