webservices系列(二)——JAX-WS文件上傳下載

  • 2019 年 10 月 30 日
  • 筆記

版權聲明:本文為部落客原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。

本文鏈接:https://blog.csdn.net/luo4105/article/details/69938342

新建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