JAX-WS - 二进制处理之MTOM(文件上传)

一、一般模式

    服务端:

import javax.jws.WebService;

@WebService
public interface UploadService {
	
	public void upload(byte[] file);
	
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.jws.WebService;
import com.xilen.jws.UploadService;

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

	@Override
	public void upload(byte[] file) {
		try {
			/**
			 * 将字节数组输出到文件
			 */
			FileOutputStream fos = new FileOutputStream(new File("test.png"));
			fos.write(file);
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
public class WsStart {
	public static void main(String[] args) {
		String address = "http://127.0.0.1:8888/upload";
		Endpoint.publish(address, new UploadServiceImpl());
	}
}
    客户端:
import java.io.File;
import java.io.IOException;
import javax.xml.ws.soap.MTOMFeature;
import org.apache.commons.io.FileUtils;
import com.xilen.jws.impl.UploadService;
import com.xilen.jws.impl.UploadServiceImplService;

public class ClientMain {
	
	public static void main(String[] args) {
		try {
			UploadServiceImplService usis = new UploadServiceImplService();
			
			UploadService service = usis.getUploadServiceImplPort();
			
			service.upload(FileUtils.readFileToByteArray(new File("G:/xl.png")));
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

    分析:

        JAX-WS - 二进制处理之MTOM(文件上传)_第1张图片


二、MTOM模式

    服务端:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.jws.WebService;
import javax.xml.ws.soap.MTOM;
import com.xilen.jws.UploadService;

/**
 * 实现类注解MTOM
 */
@WebService(endpointInterface="com.xilen.jws.UploadService", serviceName="UploadServiceImplService")
@MTOM
public class UploadServiceImpl implements UploadService{

	@Override
	public void upload(byte[] file) {
		try {
			/**
			 * 将字节数组输出到文件
			 */
			FileOutputStream fos = new FileOutputStream(new File("test.png"));
			fos.write(file);
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
    客户端:

import java.io.File;
import java.io.IOException;
import javax.xml.ws.soap.MTOMFeature;
import org.apache.commons.io.FileUtils;
import com.xilen.jws.impl.UploadService;
import com.xilen.jws.impl.UploadServiceImplService;

public class ClientMain {
	
	public static void main(String[] args) {
		try {
			UploadServiceImplService usis = new UploadServiceImplService();
			
			/**
			 * 获取远程服务代理时传入MTOMFeature实例
			 */
			UploadService service = usis.getUploadServiceImplPort(new MTOMFeature());
			
			service.upload(FileUtils.readFileToByteArray(new File("g:/xl.png")));
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
    分析:

        JAX-WS - 二进制处理之MTOM(文件上传)_第2张图片

 
三、资源

    http://download.csdn.net/detail/u013379717/7557125

 

 

你可能感兴趣的:(WebServices)