WebService (4)-axis Advanced features transfer files

Source: Internet
Author: User

Files are mainly used in a DataHandler class, which is a dedicated transmitter that can serialize files. And then we get an input stream from the transporter, and the data from the input stream is the file we sent from the client.

Eg: transfer a file from the client to the server
(1) WebService server-side method,
Public String Transfer (DataHandler handler,string fileName) {
File File = new file (fileName);
int flag = 0;
if (Handler==null | |) fileName = NULL | | "". Equals (FileName)) {
return "Failure";
}
InputStream input = null;
FileOutputStream fos = null;
try {
input = Handler.getinputstream ();
FOS = new FileOutputStream (file);
byte[] buffer = new byte[1024];
while (input.read (buffer)!=-1) {
Fos.write (buffer);
}
catch (Exception ex) {
Ex.printstacktrace ();
} finally{
if (input!=null) {
try {
Input.close ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
if (Fos!=null) {
try {
Fos.close ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
Return "File Trasfer Success" +file.getabsolutepath ();
}


(2) SERVER-CONFIG.WSDD file configuration
<deployment xmlns= "http://xml.apache.org/axis/wsdd/"
 xmlns:java = "Http://xml.apache.org/axis/wsdd/providers/java"
    type = "Java:org.apache.axis.handlers.http.URLMapper"/>
     Type= "Java:cn.com.chenlly.ssh.webservice.axis.WSTestServiceHandle" "
  <parameter name=" Status "value=" Success/>
   <!--Custom Service;
 <service name= "ws "Provider=" Java:rpc "
  <parameter name=" className "
   value=" Cn.com.chenlly.ssh.webservice.axis.WSTestServiceImpl "/>
  <parameter name=" Allowedmethods " Value= "*"/>
  <parameter name= "Scope" value= "request"/>

<responseFlow>
</responseFlow>

<requestFlow>
</requestFlow>
<!--transfer of complex objects-->
<beanmapping qname= "Mynsd:address"
Xmlns:mynsd= "Urn:addressmanager"
Languagespecifictype= "Java:cn.com.chenlly.ssh.webservice.axis.Address" >
</beanMapping>
<!--file transfer configuration-->
<operation name= "Transfer"
Qname= "Operns:transfer"
xmlns:operns= "File"
Returnqname= "Filesend"
Returntype= "Rtns:string"
xmlns:rtns= "Http://www.w3.org/2001/XMLSchema" >
<parameter name= "Handler" type= "Tns:string"
xmlns:tns= "Http://www.w3.org/2001/XMLSchema"/>
<parameter name= "FileName" type= "Myns:datahandler"
xmlns:tns= "Http://www.w3.org/2001/XMLSchema"/>
</operation>
<typemapping qname= "Myns:datahandler" xmlns:myns= "Http://fileTransfer.sample"
Languagespecifictype= "Java:javax.activation.DataHandler"
Serializer= "Org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory"
Deserializer= "Org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory"
encodingstyle= "http://schemas.xmlsoap.org/soap/encoding/"/>
</service>

<transport name= "http" >
<requestFlow>
</requestFlow>
</transport>
</deployment>
Note: You can specify metadata about special operations on the service by using the <operation> tag. This maps the Java parameter names of the methods to the specific XML names.
Specifies a specific pattern for the parameter and maps a specific XML name to a specific action. For example
<operation name= "Method" >
</operation>

(3) Client
public class Wstestserviceclient {
public static void Main (string[] args) {
Service service = new service ();
try {
Save a file under Client C disk to the server via WebService
String fileName = "D:/sample.txt";
DataHandler dh=new DataHandler (New Filedatasource (FileName));
Call call = (call) Service.createcall ();
String url = "HTTP://192.168.1.98:8082/SSHPROJECT/SERVICES/WS?WSDL";
Call.settargetendpointaddress (New Java.net.URL (URL));
Call.setoperationname (new QName (URL, "transfer"));
QName qn=new QName ("Http://fileTransfer.sample", "DataHandler");
Call.registertypemapping (Dh.getclass (), Qn,jafdatahandlerserializerfactory.class, Jafdatahandlerdeserializerfactory.class);
Call.addparameter ("S1", qn,parametermode.in);
Call.addparameter ("S2", xmltype.xsd_string,parametermode.in);
Call.setreturnclass (String.class);
String ret= (String) Call.invoke (new object[] {dh, "d:/service.txt"});
SYSTEM.OUT.PRINTLN ("Transfer success ...");
SYSTEM.OUT.PRINTLN (ret);
catch (Exception e) {
E.printstacktrace ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.