JAX-WS - 二進位制處理之MTOM(檔案上傳)

襲冷發表於2014-06-30

一、一般模式

    服務端:

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

    分析:

        


二、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();
		}
	}
}
    分析:

        

 
三、資源

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

 

 

相關文章