Skip to content

Instantly share code, notes, and snippets.

@Sennahoi
Last active July 5, 2016 11:42
Show Gist options
  • Save Sennahoi/7b0df8123a13505345d4b7aed41be903 to your computer and use it in GitHub Desktop.
Save Sennahoi/7b0df8123a13505345d4b7aed41be903 to your computer and use it in GitHub Desktop.
Tomcat 7 / Maven / JAX-WS / File upload via MTOM
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>upload.test</groupId>
<artifactId>wstest</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>wstest Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.1.3</version>
</dependency>
<!-- You get strange NPE error without this -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.stream</groupId>
<artifactId>sjsxp</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
<build>
<finalName>wstest</finalName>
</build>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">
<endpoint name="test" url-pattern="/ws/hello" implementation="upload.test.WSTestImpl"/>
</endpoints>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>ws</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ws</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
<display-name>Archetype Created Web Application</display-name>
</web-app>
package upload.test;
import java.awt.Image;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
@WebService
@SOAPBinding(style = Style.RPC)
public interface WSTest {
@WebMethod
Boing printMessage();
@WebMethod
public Boing uploadImage(byte[] data);
}
package upload.test;
import java.awt.Image;
import java.io.File;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;
import javax.xml.ws.soap.MTOMFeature;
import javax.xml.ws.soap.SOAPBinding;
public class WSTester {
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8091/wstest/ws/hello?wsdl");
QName qname = new QName("http://test.upload/", "WSTestImplService");
Service service = Service.create(url, qname);
WSTest imageServer = service.getPort(WSTest.class);
/************ test upload ****************/
// enable MTOM in client
BindingProvider bp = (BindingProvider) imageServer;
SOAPBinding binding = (SOAPBinding) bp.getBinding();
binding.setMTOMEnabled(true);
byte[] data = {};
Object ret = imageServer.uploadImage(data);
System.out.println("imageServer.uploadImage() : " + ret);
}
}
package upload.test;
import java.awt.Image;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.xml.ws.soap.MTOM;
@MTOM(enabled=true)
@WebService(endpointInterface="upload.test.WSTest")
public class WSTestImpl implements WSTest{
public Boing printMessage() {
return new Boing();
}
public Boing uploadImage(byte[] data){
System.out.println(data);
return new Boing();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment