1. 程式人生 > >webservices系列(二)——JAX-WS檔案上傳下載

webservices系列(二)——JAX-WS檔案上傳下載

新建ImgData類,存放檔案javabean

DataHandler:使用這個型別存放檔案

@XmlRootElement(name="ImaData")
@XmlAccessorType(XmlAccessType.FIELD)
public class ImgData {
	
	private Integer id;
	@XmlMimeType("application/octet-stream")
	private DataHandler imgData;	//檔案
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public DataHandler getImgData() {
		return imgData;
	}
	public void setImgData(DataHandler imgData) {
		this.imgData = imgData;
	}
}
Webservice介面,
@WebService(name="iHello2")
@SOAPBinding(style = SOAPBinding.Style.RPC)
@MTOM
public interface IHello2 {
	public void printContext();
	public ImgData getById(@WebParam(name="imgData")ImgData imgData);
}
實現類
@WebService(serviceName="HelloService",portName="HelloServicePort",
targetNamespace="http://service.lc.com/",endpointInterface="com.lc.service2.IHello2")
public class IHello2Imp implements IHello2 {

	@Resource
	private WebServiceContext context;
	
	@Override
	public void printContext() {
		MessageContext ctx = context.getMessageContext();
		Set<String> set = ctx.keySet();
		for(String key : set) {
			System.out.println("{" + key + "," + ctx.get(key) + "}");
			try {
				System.out.println("key.scope:" + ctx.getScope(key));
			} catch (Exception e) {
				System.out.println("scope not found");
			}
			
		}
	}

	@Override
	public ImgData getById(ImgData custom) {
		if(custom.getId() == 1) {
			File file = new File("f:" + File.separator + "原檔案.png");
			System.out.println(file.exists());
			custom.setImgData(new DataHandler(new FileDataSource(file)));
		}
		return custom;
	}

}
這裡需要在f盤下放一個“原檔案.png”的檔案,當然也可以改
建立webservice類SoapService2,執行
public class SoapService2 {

	public static void main(String[] args) {
		Endpoint.publish("http://localhost:8080/HelloService2", new IHello2Imp());
		System.out.println("run success");
	}

}
生成客戶端程式碼

在cmd使用wsimport -p com.lc.client2 -keep http://localhost:8080/HelloService2?wsdl


把檔案拷到eclipse對應包下
建立客戶端類SoapClient.java,執行
public class SoapClient {
	public static void main(String[] args) throws MalformedURLException {
		QName qName = new QName("http://service.lc.com/", "HelloService");
		HelloService helloService = new HelloService(new URL("http://localhost:8080/HelloService2?wsdl"), qName);
		IHello2 hello2 = helloService.getPort(IHello2.class);
		hello2.printContext();
		ImgData imgData = new ImgData();
		imgData.setId(1);
		ImgData imgData2 = hello2.getById(imgData);
		DataSource ds = imgData2.getImgData().getDataSource();
		String ctt = ds.getContentType();
		System.out.println("ContentType:" + ctt);
		try {
			InputStream is = ds.getInputStream();
			OutputStream os = new FileOutputStream("F:" + File.separator + "t1.png");
			byte[] bytes = new byte[1024];//一次讀取1024byte
			int i = 0;
			while((i = is.read(bytes)) != -1){
				os.write(bytes, 0, i);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
}
執行結果




webservices系列參考資料
[1].webservice搭建和檔案上傳下載:http://wuhongyu.iteye.com/blog/807470

[2].天氣預報和手機號資訊查詢:https://my.oschina.net/liu13430/blog/373940?fromerr=WmdtQOoY

[3].axis2建立例項:http://clq9761.iteye.com/blog/976029/

[4].axis2整合web專案:http://wangronaldo.iteye.com/blog/1456441

[5].xml配置詳情:http://blog.csdn.net/guihaijinfen/article/details/8363839