CXF Webservice MTOM无法发送文件

时间:2012-02-22 11:43:03

标签: java web-services cxf mtom

我有一个web服务,我想发送大文件。当文件小于50 MB时,它可以工作。任何人都知道可能出现什么问题?

@WebService(name = "JobbWebservice", serviceName = "JobbWebservice")
@MTOM(threshold = 3072)
@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_MTOM_BINDING)
@EndpointConfig(configName = "Seam WebService Endpoint")
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
public class JobbWebservice {
    @Consumes("multipart/form-data")
    @POST
    @WebMethod
    public  DataHandlerObject getFile(@WebParam(name = "credentials") Credentials credentials,@WebParam(name = "fileName") String fileName) throws Exception{
        JobbComponent jobbComponent = (JobbComponent)Component.getInstance("jobbComponent");
        return (DataHandlerObject)jobbComponent.getFile(fileName);
    }
}

数据处理程序对象如下所示

@XmlAccessorType(XmlAccessType.FIELD)            
public class DataHandlerObject implements DataHandlerInterface,Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -7294059438616965795L;

    /** The file name. */
    String fileName;

    /** The path. */
    String path;

    /** The data handler. */
    @XmlMimeType("application/octet-stream") 
    DataHandler dataHandler;

以及创建Datahandlerobject的功能如下

public DataHandlerInterface getFile(String fileName) throws SyllException{
        try{        

            File file = new File(filePath+"\\"+fileName);
            DataHandler dataHandler = new DataHandler(new FileDataSource(file));

            DataHandlerObject dataHandlerObject = new DataHandlerObject();
            dataHandlerObject.setDataHandler(dataHandler);

            dataHandlerObject.setFileName(file.getName());              

            return dataHandlerObject;
        }catch(Exception e){
            throw new SyllException("Can't send file");
        }
    }

客户端部分如下:

    JobbWebservice_Service jobbWebservice_Service=  new JobbWebservice_Service();
    JobbWebservice jobbWebservice =  jobbWebservice_Service.getJobbWebservicePort(new MTOMFeature(true,3072));
    Credentials credentials = new Credentials();
    DataHandlerObject dataHandler = jobbWebservice.getFile(credentials, "test.java");

    DataHandler handler = dataHandler.getDataHandler();
    try{
        InputStream is = handler.getInputStream();
        int nRead; 
        byte[] data = new byte[3072];  
        File f=new File("F:\\temp\\downloaded\\");
        f.mkdirs();
        f = new File(f,dataHandler.getFileName());

        OutputStream out=new FileOutputStream(f);
        while ((nRead = is.read(data, 0, data.length)) != -1) {   
            out.write(data,0,nRead);
        }  
        out.close();
        is.close();

    }catch (Exception e) {
        e.printStackTrace();
    }

当我在客户端运行时,我收到此错误:

12:32:42,916 WARN  [org.apache.cxf.phase.PhaseInterceptorChain] Interceptor for JobbWebservice # getFile has thrown exception, unwinding now: org.apache.cxf.interceptor.Fault: Could not write attachments.
    at org.apache.cxf.interceptor.AttachmentOutInterceptor$AttachmentOutEndingInterceptor.handleMessage(AttachmentOutInterceptor.java:98) [:2.3.1-patch-01]
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:255) [:2.3.1-patch-01]
    at org.apache.cxf.interceptor.OutgoingChainInterceptor.handleMessage(OutgoingChainInterceptor.java:77) [:2.3.1-patch-01]
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:255) [:2.3.1-patch-01]
    at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:113) [:2.3.1-patch-01]
    at org.apache.cxf.transport.servlet.ServletDestination.invoke(ServletDestination.java:97) [:2.3.1-patch-01]

0 个答案:

没有答案