CXF Webservice MTOM 无法发送文件

发布于 2025-01-08 04:20:41 字数 3890 浏览 3 评论 0原文

我有一个网络服务,我想用它发送大文件。当文件小于 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;

,用于创建数据处理程序对象的函数如下

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]

I have a webservice which i would like to send large files with. It works when the file ar small below 50 MB. Anyone got any idea what could be wrong?

@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);
    }
}

the data Handler object looks like this

@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;

and the function for creating the Datahandlerobject is the following

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");
        }
    }

Client part is the following:

    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();
    }

When i run it on client side i get this error :

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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文