CXF File Upload With SOAP MTOM

Create a Service Interface

This service interface will defines which methods of web service, to be invoked by the client

package com.student;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface UploadService {
  
void uploadFile(@WebParam(name = "file") FileUploader file);
}

Implement the Service Interface

Here we will implement the service interface created on the previous step

package com.student;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.activation.DataHandler;
import javax.jws.WebService;

@WebService(endpointInterface = "com.student.UploadService", serviceName = "UploadService")
public class UploadServiceImpl implements UploadService {

  
public void uploadFile(FileUploader Dfile) {

     
DataHandler handler = Dfile.getDfile();
     
try {
        
InputStream is = handler.getInputStream();

         OutputStream os =
new FileOutputStream(new File("C:/uploads/" + Dfile.getName() + "." + Dfile.getFileType()));
        
byte[] b = new byte[100000];
        
int bytesRead = 0;
        
while ((bytesRead = is.read(b)) != -1) {
           
os.write(b, 0, bytesRead);
        
}
        
os.flush();
         os.close
();
         is.close
();

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

   }
}

cxf.xml

CXF is using Spring internally, Finding classes by spring we need to add service implementation class on "jaxws:endpoint" tag

we also added following line in order to enable the MTOM support inside CXF framework

<entry key="mtom-enabled" value="true" /> 
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
	<jaxws:endpoint id="uploadfile" implementor="com.student.UploadServiceImpl"
		address="/Upload">
		<jaxws:properties>
			<entry key="mtom-enabled" value="true" />
		</jaxws:properties>
	</jaxws:endpoint>
</beans>





Previous 1 2 3 Next








4 Responses to "CXF File Upload With SOAP MTOM"
  1. skanda 2013-04-18 10:37:17.0
  1. skanda 2013-04-19 10:37:17.0
  1. Bob Rivers 2013-04-20 10:37:17.0
  1. FerminLucky 2013-04-21 10:37:17.0

Your email address will not be published. Required fields are marked *