简体   繁体   中英

Downloading file via Web Service using Apache CXF

I try to develop Web Service which send file from server to client. I use Apache CXF for developing Web Serivce. Application is Spring based. First I tried to write simple test code:

applicationContext.xml

<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

<bean class="com.llth.paymentgateway.webservice.PaymentGatewayReportingWebServiceImpl" id="paymentGatewayReportingWebService"/>

<jaxws:endpoint id="reportingWebService" implementor="#paymentGatewayReportingWebService" address="/ReportingWebService">
    <jaxws:properties>
        <entry key="mtom-enabled" value="true"/>
    </jaxws:properties>
</jaxws:endpoint>

PaymentGatewayReportingWebService.java Web Service inteface

@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface PaymentGatewayReportingWebService {

    @WebMethod(operationName = "downloadFile")
    public DataHandler downloadFile(String fileName);

}

PaymentGatewayReportingWebServiceImpl.java implementation of PaymentGatewayReportingWebService inteface

@WebService(endpointInterface = "com.llth.paymentgateway.webservice.PaymentGatewayReportingWebService")
public class PaymentGatewayReportingWebServiceImpl implements PaymentGatewayReportingWebService {

    @Override
    @XmlMimeType("application/octet-stream")
    public DataHandler downloadFile(String fileName) {
        FileDataSource dataSource = new FileDataSource(fileName);

        return new DataHandler(dataSource);
    }
}

When I import and execute Web Service method by soapUI I recevie the following response (copied from RAW tab)

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: multipart/related; type="application/xop+xml"; boundary="uuid:9f003136-a7e0-4634-8032-51c1ba701776"; start="<root.message@cxf.apache.org>"; start-info="text/xml";charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 12 Mar 2013 12:35:06 GMT


--uuid:9f003136-a7e0-4634-8032-51c1ba701776
Content-Type: application/xop+xml; charset=UTF-8; type="text/xml";
Content-Transfer-Encoding: binary
Content-ID: <root.message@cxf.apache.org>

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:downloadFileResponse xmlns:ns1="http://webservice.paymentgateway.llth.com/"><return><xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:c0836641-6f5f-46ac-9e01-d89eaceafe49-1@cxf.apache.org"/></return></ns1:downloadFileResponse></soap:Body></soap:Envelope>
--uuid:9f003136-a7e0-4634-8032-51c1ba701776
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-ID: <c0836641-6f5f-46ac-9e01-d89eaceafe49-1@cxf.apache.org>

яШяб'«Exif
and some binary data here

soapUI doesn't download anything. I don't know if everything is right or not. Should soapUI begin download the file? If soapUI should download the file then something is wrong. If yes, what is wrong?

In fact soapUI is giving you the right answer. That "some binary data here" is the file you asked for

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM