WEB Service传递附件如何做啊(axis)?
我的axis版本较低是1.1的,
想传递附件 ,wsdd文件怎么配置啊?
<schema targetNamespace="urn:KxItfService">
<import namespace="http://xml.apache.org/xml-soap"/>
<import namespace="http://schemas.xmlsoap.org/soap/encoding/"
/>
<complexType name="AttachFileItem">
<sequence>
<element name="attachFile" nillable="true" type="apachesoap:DataHandler"/>
<element name="fileName" nillable="true" type="soapenc:string"/>
</sequence>
</complexType>
另外,<element name="attachFile" nillable="true" type="apachesoap:DataHandler"/>这一个是如何配置得到的啊?
我的显示是tns1:DataHandler
[解决办法]
service端:
package org.sky.hello;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.activation.DataHandler;
import org.apache.axiom.attachments.Attachments;
import org.apache.axis2.context.MessageContext;
public class AttachmentService {
public String uploadFile(String name, String attchmentID) throws IOException
{
MessageContext msgCtx = MessageContext.getCurrentMessageContext();
Attachments attachment = msgCtx.getAttachmentMap();
DataHandler dataHandler = attachment.getDataHandler(attchmentID);
File file = new File(
name);
FileOutputStream fileOutputStream = new FileOutputStream(file);
dataHandler.writeTo(fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
return "File saved succesfully.";
}
}
service.xml的配置:
<service name="AttachmentService" >
<parameter name="ServiceClass">org.sky.hello.AttachmentService</parameter>
<operation name="uploadFile">
<actionMapping>urn:uploadFile</actionMapping>
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</operation>
</service>
要用rpcmessagereceiver
客户端:
package org.sky;
import java.io.File;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAP11Constants;
import org.apache.axiom.soap.SOAPBody;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.OperationClient;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.wsdl.WSDLConstants;
public class AttachmentClient {
private static EndpointReference targetEPR = new EndpointReference(
"http://127.0.0.1:8080/AxisProject/services/AttachmentService");
public static void main(String[] args) throws Exception {
new AttachmentClient().transferFile();
}
public void transferFile() throws Exception {
String filePath = "d:/test.gif";
String destFile = "d:/Axis2Temp/test.gif";
Options options = new Options();
options.setTo(targetEPR);
options.setProperty(Constants.Configuration.ENABLE_SWA,
Constants.VALUE_TRUE);
options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
// Increase the time out when sending large attachments
options.setTimeOutInMilliSeconds(10000);
options.setTo(targetEPR);
options.setAction("urn:uploadFile");
// assume the use runs this sample at
// <axis2home>/samples/soapwithattachments/ dir
StringBuilder repositoryPath = new StringBuilder();
repositoryPath.append(this.getClass().getResource("/").getPath());
repositoryPath.append("repository");
System.out.println(repositoryPath.toString());
ConfigurationContext configContext = ConfigurationContextFactory
.createConfigurationContextFromFileSystem(repositoryPath.toString(),
null);
ServiceClient sender = new ServiceClient(configContext, null);
sender.setOptions(options);
OperationClient mepClient = sender
.createClient(ServiceClient.ANON_OUT_IN_OP);
MessageContext mc = new MessageContext();
FileDataSource fileDataSource = new FileDataSource(new File(filePath));
// Create a dataHandler using the fileDataSource. Any implementation of
// javax.activation.DataSource interface can fit here.
DataHandler dataHandler = new DataHandler(fileDataSource);
String attachmentID = mc.addAttachment(dataHandler);
SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();
SOAPEnvelope env = fac.getDefaultEnvelope();
OMNamespace omNs = fac.createOMNamespace("http://hello.sky.org", "");
OMElement uploadFile = fac.createOMElement("uploadFile", omNs);
OMElement nameEle = fac.createOMElement("name", omNs);
nameEle.setText(destFile);
OMElement idEle = fac.createOMElement("attchmentID", omNs);
idEle.setText(attachmentID);
uploadFile.addChild(nameEle);
uploadFile.addChild(idEle);
env.getBody().addChild(uploadFile);
System.out.println("message====" + env);
mc.setEnvelope(env);
mepClient.addMessageContext(mc);
mepClient.execute(true);
MessageContext response = mepClient
.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
SOAPBody body = response.getEnvelope().getBody();
OMElement element = body.getFirstElement().getFirstChildWithName(
new QName("http://hello.sky.org", "return"));
System.out.println(element.getText());
}
}
[解决办法]
我上面用的是axis2 1.4.2
下面给出用JAX-WS实现的传递附件的例子:
service端
package ctsjavacoe.ws.fromjava;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Holder;
import java.io.*;
@WebService
public class MTOMSimple {
@WebMethod
public void echoData(Holder<byte[]> data) {
OutputStream os = null;
ByteArrayInputStream bin = null;
try {
bin = new ByteArrayInputStream(data.value);
if (data.value != null)
os = new FileOutputStream("D:/upload/jaxwsupload/echoData.jpg");
byte[] bytes = new byte[1024];
int c;
while ((c = bin.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
os.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
os = null;
}
} catch (Exception e) {
}
try {
if (bin != null) {
bin.close();
bin = null;
}
} catch (Exception e) {
}
}
}
}
客户端:
package ctsjavacoe.ws.fromjava;
import com.sun.xml.ws.developer.JAXWSProperties;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Holder;
import javax.xml.ws.soap.SOAPBinding;
import javax.xml.ws.soap.MTOMFeature;
import javax.xml.transform.stream.StreamSource;
import javax.activation.DataHandler;
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.awt.*;
import java.util.Arrays;
public class MTOMSimplePollingClient {
/**
* @param args
*/
public static void testEcho(MTOMSimple port) throws Exception {
byte[] bytes = AttachmentHelper.getImageBytes(getImage("carrier.jpg"),
"image/jpeg");
Holder<byte[]> image = new Holder<byte[]>(bytes);
port.echoData(image);
if (image.value != null)
System.out.println("SOAP 1.1 testEcho() PASSED!");
else
System.out.println("SOAP 1.1 testEcho() FAILED!");
}
private static String getDataDir () {
return "d:/";
}
private static Image getImage(String imageName) throws Exception {
String location = getDataDir() + imageName;
System.out.println("image location=====>"+location);
return javax.imageio.ImageIO.read(new File(location));
}
public static void main(String[] args) {
try {
MTOMSimple port = new MTOMSimpleService()
.getMTOMSimplePort(new MTOMFeature());
if (port == null) {
System.out.println("TEST FAILURE: Couldnt get port!");
System.exit(-1);
}
// test echo
testEcho(port);
} catch (Exception ex) {
System.out.println("SOAP 1.1 MtomApp FAILED!");
ex.printStackTrace();
}
}
}