openadaptor1--transferring large files based on WebService

Source: Internet
Author: User
Tags getmessage throwable wsdl

<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255,255,255) ">openadaptor support Xfire and CXF development WebService, so the development of CXF WebService, based on MyEclipse development is relatively easy. Reference online webrvice Transfer Large file example modify Http://blog.csdn.net/kongxx/article/details/7540930</span>

1. The first is a myfile class that encapsulates the server-side file path, the client file path, and the byte array to be transferred.

package Com.cattsoft.baseplatform.webservice.fileltransfer;public class MyFile {private String Clientfile;private string serverfile;//Statistics total uploads downloaded bytes Private long position;//each uploaded or downloaded byte array private byte[] bytes;public String getclientfile () { return clientfile;} public void Setclientfile (String clientfile) {this.clientfile = Clientfile;} Public String Getserverfile () {return serverfile;} public void Setserverfile (String serverfile) {this.serverfile = Serverfile;} Public long getPosition () {return position;} public void SetPosition (long position) {this.position = position;} Public byte[] GetBytes () {return bytes;} public void setbytes (byte[] bytes) {this.bytes = bytes;}} 


2. Web service interface for file transfer

Package Com.cattsoft.baseplatform.webservice.fileltransfer;import Javax.jws.webmethod;import Javax.jws.WebService ; @WebServicepublic interface Filetransferservice {@WebMethodvoid uploadfile (string address, string Clientfile, String Serverfile) throws filetransferexception; @WebMethodvoid UploadFile1 (MyFile MyFile, String address) throws Filetransferexception, @WebMethodvoid uploadFile2 (MyFile MyFile) throws filetransferexception; @WebMethodvoid DownloadFile (string address, String clientfile, String serverfile) throws filetransferexception; @WebMethodvoid DownloadFile1 (MyFile MyFile, String address) throws Filetransferexception; @WebMethodMyFile DownloadFile2 (MyFile MyFile) throws filetransferexception;}


3. Web Service Interface Implementation classes for file transfers, mainly for some flow operations

Package Com.cattsoft.baseplatform.webservice.fileltransfer;import Java.io.file;import Java.io.FileInputStream; Import Java.io.ioexception;import java.io.inputstream;import Java.io.outputstream;import Java.util.Arrays;import Javax.jws.webservice;import Org.apache.commons.io.fileutils;import Org.apache.commons.io.ioutils;import Org.apache.cxf.jaxws.JaxWsProxyFactoryBean; @WebService (endpointinterface = " Com.cattsoft.baseplatform.webservice.fileltransfer.FileTransferService ", ServiceName =" Filetransferservice ") public class Filetransferserviceimpl {//byte[] Transfer of files, each transfer of 1024*1024, after transfer myfile.setposition record total bytes transferred public void UploadFile (string address, String clientfile, String serverfile) throws Filetransferexception {InputStream is = null;try { MyFile MyFile = new MyFile (); is = new FileInputStream (clientfile); byte[] bytes = new byte[1024 * 1024];while (TRUE) {int S ize = Is.read (bytes);//The read data from the input stream is stored in the buffered array bytes, returning the number of bytes read until the end of the file is detected. Reads at most equivalent to read (bytes,//0, Bytes.length) if (size <= 0) {//returns-1 means end of file BReak;} byte[] fixedbytes = arrays.copyofrange (bytes, 0, size);//Copy bytes array to fixedbytes array, from 0 to Sizemyfile.setclientfile ( Clientfile); Myfile.setserverfile (serverfile); myfile.setbytes (fixedbytes); UploadFile1 (myFile, address); Myfile.setposition (myfile.getposition () + fixedbytes.length);//number of bytes transferred}} catch (IOException e) {throw new Filetransferexception (E.getmessage (), E); Finally {ioutils.closequietly (IS);}} public void UploadFile1 (MyFile MyFile, String address) throws Filetransferexception {Jaxwsproxyfactorybean Factorybean = New Jaxwsproxyfactorybean (); factorybean.setaddress (address); Factorybean.setserviceclass ( Filetransferservice.class); Object obj = Factorybean.create (); Filetransferservice service = (filetransferservice) obj;service.uploadfile2 (myFile);}  public void UploadFile2 (MyFile MyFile) throws filetransferexception {OutputStream os = null;try {if (Myfile.getposition () ! = 0) {//Use Apache Commons-io FileUtils Tool class//open stream, if there is no creation file and its directory structure, the second parameter indicates whether the file stream is an append mode OS = Fileutils.openoutputstream (new File (Myfile.getserverfile ()), true);} else {OS = Fileutils.openoutputstream (New File (Myfile.getserverfile ()), false);} Os.write (Myfile.getbytes ());} catch (IOException e) {throw new Filetransferexception (E.getmessage (), e);} finally {ioutils.closequietly (OS)}} public void DownloadFile (string address, String clientfile, String serverfile) throws Filetransferexception {MyFile MyFile = new MyFile (); Myfile.setserverfile (serverfile); Long position = 0;while (true) {myfile.setposition (position); MyFile = DownloadFile1 (myFile, address), if (Myfile.getbytes (). length <= 0) {break;} OutputStream OS = null;try {if (position! = 0) {OS = Fileutils.openoutputstream (new File (Clientfile), true);} else {os = F Ileutils.openoutputstream (New File (Clientfile), false);} Os.write (Myfile.getbytes ());} catch (IOException e) {throw new Filetransferexception (E.getmessage (), e);} finally {ioutils.closequietly (OS);} Position + = Myfile.getbytes (). length;}} Public MyFile DownloadFile1 (MyFile MyFile, String address) throws filetransferexception {Jaxwsproxyfactorybean Factorybean = new Jaxwsproxyfactorybean (); Factorybean.setaddress ( address); Factorybean.setserviceclass (filetransferservice.class); Object obj = Factorybean.create (); Filetransferservice service = (filetransferservice) Obj;return service.downloadfile2 (myFile);} Public MyFile downloadFile2 (MyFile MyFile) throws Filetransferexception {InputStream are = null;try {is = new fileinputstre AM (Myfile.getserverfile ()); Is.skip (Myfile.getposition ()); byte[] bytes = new byte[1024 * 1024];int size = is.read (bytes) if (Size > 0) {byte[] fixedbytes = arrays.copyofrange (bytes, 0, size); Myfile.setbytes (fixedbytes);} else {Myfile.setb Ytes (new byte[0]);}} catch (IOException e) {throw new Filetransferexception (E.getmessage (), e);} finally {ioutils.closequietly (IS);} return myFile;}}


4. A Simple File transfer exception class

Package Com.cattsoft.baseplatform.webservice.fileltransfer;public class Filetransferexception extends Exception { Private static final Long Serialversionuid = 1l;public filetransferexception () {super ();} Public filetransferexception (String message, Throwable cause) {Super (message, cause);} Public filetransferexception (String message) {super (message);} Public filetransferexception (Throwable cause) {super (cause);}}


5. The following is the server class used to publish the Web service

Package Com.cattsoft.baseplatform.webservice.fileltransfer;import Javax.xml.ws.endpoint;public Class filetransferserver {public static void main (string[] args) throws Exception {endpoint.publish ("http://localhost:9000/ Ws/jaxws/filetransferservice ", New Filetransferserviceimpl ());//Endpoint.publish (Args[0], new Filetransferserviceimpl ());}}


6. Finally, the client class is used to send file uploads and download requests.

Package com.cattsoft.baseplatform.webservice.fileltransfer;/* * UploadFile is uploaded from Clientfile to Serverfile * DownloadFile is transmitted from Serverfile to Clientfile * Parameters args[0], args[1],args[2] in turn, address, Clientfile, Serverfile */public class filetransferclient {private static final String address = "Http://192.168.100.169:9000/ws/jaxws/fileTransferService1" ;p rivate static final String clientfile = "F:/ftp/test.txt";p rivate static final String serverfile = "d:/share/portal_esb/ Ftp/test.txt ";p ublic static void Main (string[] args) throws Exception {Long start = System.currenttimemillis (); Filetransferserviceimpl FTS = new Filetransferserviceimpl () Fts.uploadfile (Args[0], args[1], args[2]);//DownloadFile (Args[0], args[1], args[2]); Long stop = System.currenttimemillis (); System.out.println ("Time:" + (Stop-start));}}


7. Configuring the Openadaptor File

<?xml version= "1.0" encoding= "UTF-8"?><!--$Id: Step01.xml 696 2007-06-27 13:04:36z higginse $ $HeadURL: HTTPS ://openadaptor3.openadaptor.org/svn/openadaptor3/tags/3.4.7/example/tutorial/step01.xml $--><beans xmlns= " Http://www.springframework.org/schema/beans "xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance "Xsi:schemal ocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.0.xsd "> <description><!  [cdata[Adaptor for Step 1 of the tutorial. ]]></description> <bean id= "Adaptor" class= "Org.openadaptor.core.adaptor.Adaptor" > <property name = "Messageprocessor" ref= "Router"/> </bean> <bean id= "Router" class= "Org.openadaptor.core.router.Router" > <property name= "processors" > <list> <ref bean= "Reader"/><ref bean= "Buywriter"/&gt      ; </list> </property> </bean> <bean id= "Reader" class= "Org.openadaptor.auxil.connector.soap.WebServiceCXFReadConnector" > <property name= "wsendpoint" value= "http ://localhost:9000/ws/jaxws/filetransferservice1?wsdl "/> <property name=" ServiceName "> <bean class=" Javax.xml.namespace.QName "> <constructor-arg type=" java.lang.String "index=" 0 "value=" http://filetransfer.cx fstudy.garbagecan.googlecode.com/"></constructor-arg> <constructor-arg type=" java.lang.String "index= "1" value= "UploadFile" ></constructor-arg> </bean> </property><property name= "Parameters" &G T;<list><value type= "Java.lang.String" >http://localhost:9000/ws/jaxws/filetransferservice1</ Value><value type= "java.lang.String" >f:/ftp/test.txt</value><value type= "java.lang.String" > f:/ftp1/test1.txt</value></list> </property> </bean> <bean id= "Writer" class= "Org.openad Aptor.auxil.connector.soap.WebServiceCXFWriteConnector "> <Property Name= "Endpoint" value= "HTTP://192.168.223.142:9763/ADDSERVICE_1.0.0/SERVICES/ADD_SERVICE?WSDL"/>< Property Name= "MethodName" value= "Add"/></bean> <bean id= "Buywriter" class= " Org.openadaptor.auxil.connector.iostream.writer.FileWriteConnector "> <property name=" filename "value=" Output/1.txt "/> </bean></beans>



openadaptor1--transferring large files based on WebService

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.