无法使用MTOM和Axis2存根从webservice下载文件

时间:2014-11-21 17:59:27

标签: file axis2 transfer mtom axiom

我正在尝试使用MTOM和ADB从我的Axis2 webservice服务器下载文件。 如果我不在服务器端和客户端都启用MTOM,我可以下载该文件。任何建议或代码示例都会很好:)

客户端

ServerWSStub stub = new ServerWSStub();
stub._getServiceClient().getOptions().setProperty(Constants.Configuration.ENABLE_MTOM,Constants.VALUE_TRUE);

服务器端axis2.xml

<parameter name="enableMTOM">optional</parameter>

这是我的服务器

public DataHandler download(String konum) throws Exception {
        System.out.println("The filePath for download: " + konum);
        FileDataSource dataSource = new FileDataSource(konum);
        DataHandler datahandler = new DataHandler(dataSource);

        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace ns = fac.createOMNamespace("http://benim.projem.org/dosya", "dosyam");
        OMText textData = fac.createOMText(datahandler, true);
        OMElement ele = fac.createOMElement("sonuc", ns);
        ele.addChild(textData);
        System.out.println(ele);
        return datahandler;

这是我的客户

ServerWSStub stub = new ServerWSStub();          

//stub._getServiceClient().getOptions().setProperty(Constants.Configuration.ENABLE_MTOM,Constants.VALUE_TRUE); 

//when uncommented i get java.lang.NoClassDefFoundError: org/apache/james/mime4j/MimeException 
//while trying to invoke _operationClient.execute(true); in ServerWSStub 
//I guess it is because of wrong unparsing


Download download = new Download();
download.setKonum(konum);
try {
    DownloadResponse downloadResponse = stub.download(download);
    DataHandler dh =(DataHandler) downloadResponse.get_return();
    File file = new File("C:/dosya/"+fileNameType);

    if (!file.getParentFile().exists())
        file.getParentFile().mkdirs();
    if(!file.exists()){
        file.createNewFile();
    }
    FileOutputStream fileOutputStream = new FileOutputStream(file);

    dh.writeTo(fileOutputStream);
    fileOutputStream.flush();
    fileOutputStream.close();
} catch (ServerWSExceptionException e) {
    e.printStackTrace();
}

任何

2 个答案:

答案 0 :(得分:1)

我猜终于得到了解决方案。在客户端获取整个文件之前,Stream关闭了,这就是为什么我首先使用了getOptions()。setTimeOutInMilliSeconds(10000)方法,但它也没用,然后在我评论的Stub文件中

_messageContext.getTransportOut().getSender().cleanup(_messageContext);//look for the method's finally part

部分,以便在大型文件传输期间,流没有被关闭,我可以下载整个文件,没有任何愚蠢的例外:)

--MIMEBoundary_e56e8a77b94fbdd7678582aa5ca53f50b1d56c0d828499ea
Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
Content-Transfer-Encoding: binary
Content-ID: <0.146e8a77b94fbdd7678582aa5ca53f50b1d56c0d828499ea@apache.org>

<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns:downloadResponse xmlns:ns="http://servis.ws.projem.tez.benim"><ns:return><xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:1.046e8a77b94fbdd7678582aa5ca53f50b1d56c0d828499ea@apache.org" /></ns:return></ns:downloadResponse></soapenv:Body></soapenv:Envelope>
--MIMEBoundary_e56e8a77b94fbdd7678582aa5ca53f50b1d56c0d828499ea
Content-Type: text/plain
Content-Transfer-Encoding: binary
Content-ID: <1.046e8a77b94fbdd7678582aa5ca53f50b1d56c0d828499ea@apache.org>
binary code here

答案 1 :(得分:0)

只需将 apache-mime4j-core-0.7.2.jar 添加到服务(服务器)端的 WEB-INF / lib 即可。可以找到jar here