CXF file transfers are implemented through MTOM. MTOM (SOAP message transmission optimization mechanism) SOAP messages transmission optimization mechanism, you can send binary data in the SOAP message. MTOM allows the large data elements contained in the message to be externally instantiated and delivered as binary data without any special encoding with the message. Compared with the transfer of binary to Base64, MTOM has higher transmission efficiency.
File transfer wrapper class
CXF file transfer datahandler only binary data, no filename, file type, file size, etc., requires additional transfer parameters. Custom file transport wrapper classes are typically used to transfer binary and extra parameters.
PackageCom.rvho.cxfserver;ImportJavax.activation.DataHandler;ImportJavax.xml.bind.annotation.XmlMimeType;ImportJavax.xml.bind.annotation.XmlType;/** * cxf upload and download file object wrapper class due to CXF DataHandler cannot get file name and file type, need to be uploaded and downloaded with file name * * @author [email protected] */@XmlType(name ="Cxffilewrapper") Public class cxffilewrapper { //File name PrivateString FileName;//File name extension PrivateString fileextension;//File binary data PrivateDataHandler file; PublicStringGetFileName() {returnFileName; } Public void Setfilename(String fileName) { This. filename = filename; } PublicStringgetfileextension() {returnFileExtension; } Public void setfileextension(String fileextension) { This. fileextension = FileExtension; }//Note This field is a binary stream @XmlMimeType("Application/octet-stream") PublicDataHandlerGetFile() {returnFile } Public void Setfile(DataHandler file) { This. File = file; }}
Service side
File Transfer Service Interface
Packagecom.rvho.cxfserver.ws;ImportJavax.jws.WebMethod;ImportJavax.jws.WebParam;ImportJavax.jws.WebResult;ImportJavax.jws.WebService;ImportCom.rvho.cxfserver.CxfFileWrapper;@WebService(name ="Filews", targetnamespace ="Http://www.tmp.com/services/file") Public interface filews { /** * File upload * @param File Upload wrapper class * @return Upload successful return true, upload failed to return false. */ @WebMethod BooleanUpload@WebParam(name ="File") cxffilewrapper file);/** * File download * @return File * * * @WebMethodCxffilewrapper download ();}
File Transfer Service Implementation class
PackageCom.rvho.cxfserver.ws.impl;ImportJava.io.BufferedOutputStream;ImportJava.io.File;ImportJava.io.FileOutputStream;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJavax.activation.DataHandler;ImportJavax.activation.DataSource;ImportJavax.activation.FileDataSource;ImportJavax.jws.WebService;ImportOrg.springframework.stereotype.Service;ImportCom.rvho.cxfserver.CxfFileWrapper;ImportCom.rvho.cxfserver.ws.FileWS;@WebService(Endpointinterface ="Com.rvho.cxfserver.ws.FileWS", PortName ="Filewsport", ServiceName ="Filewsservice", targetnamespace ="Http://www.tmp.com/services/file")@Service("Filews") Public class Filewsimpl implements filews { @Override Public Boolean Upload(Cxffilewrapper file) {Booleanresult =true; OutputStream OS =NULL; InputStream is =NULL; Bufferedoutputstream BOS =NULL;Try{is = File.getfile (). getInputStream ();//Where files are saved on the serverFile dest =NewFile ("D:\\dev\\tmp\\upload\\"+ File.getfilename ()); OS =NewFileOutputStream (dest); BOS =NewBufferedoutputstream (OS);byte[] buffer =New byte[1024x768];intLen =0; while(len = is.read (buffer))! =-1) {bos.write (buffer,0, Len); } bos.flush (); }Catch(Exception e) {E.printstacktrace (); result =false; }finally{if(Bos! =NULL){Try{Bos.close (); }Catch(Exception e) { } }if(OS! =NULL){Try{Os.close (); }Catch(Exception e) { } }if(Is! =NULL){Try{Is.close (); }Catch(Exception e) { } } }returnResult }@Override PublicCxffilewrapperDownload() {//path to download files //string FilePath = "d:\\dev\\tmp\\upload\\ administrative region. txt";String FilePath ="E:\\tddownload\\xme_5.0.517.exe"; Cxffilewrapper FileWrapper =NewCxffilewrapper (); Filewrapper.setfilename ("Xme_5.0.517.exe"); Filewrapper.setfileextension ("EXE"); DataSource Source =NewFiledatasource (NewFile (FilePath)); Filewrapper.setfile (NewDataHandler (source));returnFileWrapper; }}
Server configuration file, you need to turn on MTOM in the endpoint configuration.
<jaxws:endpoint id="Filewsendpoint" implementor="#fileWS" Address="/file"> <jaxws:ininterceptors> <Bean class="Org.apache.cxf.interceptor.LoggingInInterceptor"> </Bean> </jaxws:ininterceptors> <jaxws:outinterceptors> <Bean class="Org.apache.cxf.interceptor.LoggingOutInterceptor"></Bean> </jaxws:outinterceptors> <jaxws:properties> <!--open Mtom -- <entry key="mtom_enabled" value="true"></Entry> </jaxws:properties></jaxws:endpoint>
Client
Client download file
Jaxwsproxyfactorybean factory =NewJaxwsproxyfactorybean (); Factory.setserviceclass (Filews.class); Factory.setaddress ("Http://localhost:8280/cxfserver/services/file"); Filews filews = factory.create (Filews.class); Cxffilewrapper filewrapper = Filews.download (); OutputStream OS =NULL; InputStream is =NULL; Bufferedoutputstream BOS =NULL;Try{is = Filewrapper.getfile (). getInputStream ();//Where the file is saved at the clientFile dest =NewFile ("D:\\dev\\tmp\\download\\"+ Filewrapper.getfilename ()); OS =NewFileOutputStream (dest); BOS =NewBufferedoutputstream (OS);byte[] buffer =New byte[1024x768];intLen =0; while(len = is.read (buffer))! =-1) {bos.write (buffer,0, Len); } bos.flush (); System.out.println ("Download Complete");}Catch(IOException e) {E.printstacktrace ();}finally{if(Bos! =NULL){Try{Bos.close (); }Catch(Exception e) { } }if(OS! =NULL){Try{Os.close (); }Catch(Exception e) { } }if(Is! =NULL){Try{Is.close (); }Catch(Exception e) { } }}
Client upload File
Jaxwsproxyfactorybean factory =NewJaxwsproxyfactorybean (); Factory.setserviceclass (Filews.class); Factory.setaddress ("Http://localhost:8280/cxfserver/services/file"); Factory.getininterceptors (). Add (NewOrg.apache.cxf.interceptor.LoggingInInterceptor ()); Factory.getoutinterceptors (). Add (NewCom.rvho.cxfclient.interceptor.AuthAddInterceptor ()); Factory.getoutinterceptors (). Add (NewOrg.apache.cxf.interceptor.LoggingOutInterceptor ()); Filews filews = factory.create (Filews.class); Cxffilewrapper FileWrapper =NewCxffilewrapper (); Filewrapper.setfilename ("Xme_5.0.517.exe"); Filewrapper.setfileextension ("EXE"); String FilePath ="E:\\tddownload\\xme_5.0.517.exe";//string filePath = "e:\\temp\\ project output document template. zip";DataSource Source =NewFiledatasource (NewFile (FilePath)); Filewrapper.setfile (NewDataHandler (source));BooleanSuccess = Filews.upload (FileWrapper); System.out.println (success?"Upload success!" ":"Upload failed!" ");
Precautions
CXF cannot transfer large files, otherwise it will prompt for memory overflow.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
WebService framework CXF Combat one transfer file (vi)