加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

jax-ws 文件下载

发布时间:2020-12-16 22:44:57 所属栏目:安全 来源:网络整理
导读:1.新建接口:FileServer import javax.activation.DataHandler;import javax.jws.WebMethod;import javax.jws.WebService;import javax.jws.soap.SOAPBinding;import javax.jws.soap.SOAPBinding.Style;/** * * @author Administrator TODO */@WebService@SOA


1.新建接口:FileServer

import javax.activation.DataHandler;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

/**
 * 
 * @author Administrator TODO
 */
@WebService
@SOAPBinding(style = Style.RPC)
public interface FileServer {
	// download a File from server
	@WebMethod
	public DataHandler downloadFile(String fileName);
}

2.实现类:

package com.it.filedown;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.jws.WebService;
import javax.xml.ws.soap.MTOM;

/**
 * 
 * @author Administrator TODO
 */
@MTOM
@WebService(endpointInterface = "com.it.filedown.FileServer")
public class FileServerImpl implements FileServer {

	public DataHandler downloadFile(String fileName) {
		FileDataSource dataSource = new FileDataSource("c:/test/"+fileName);
		DataHandler fileDataHandler = new DataHandler(dataSource);
		return fileDataHandler;
	}

}

4.发布类:FileDownServerPublisher

package com.it.filedown;

import javax.xml.ws.Endpoint;

/**
 * 
 * @author Administrator TODO
 */
public class FileDownServerPublisher {
	public static void main(String[] args) {
		// 第一个参数是发布的URL
		// 第二个参数是SIB实现
		Endpoint.publish("http://127.0.0.1:10101/filedown",new FileServerImpl());
	}
}

3.打开cmd命令窗口切换到空白目录,输入:wsimport -keep http://localhost:10101/filedown?wsdl


4.复制生成的类到客户端的javaproject新建一个client类:

package com.it.client;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.it.filedown.FileServer;

/**
 * 
 * @author Administrator TODO
 */
public class FileDownClient {
	public static void main(String[] args) {
		try {
			URL url = new URL("http://localhost:10101/filedown?wsdl");
			QName qname = new QName("http://filedown.it.com/","FileServerImplService");
			Service service = Service.create(url,qname);
			FileServer fileServer = service.getPort(FileServer.class);
			byte[]  bytes = fileServer.downloadFile("123.txt");
			FileOutputStream outputStream = new FileOutputStream("E:/test.txt");
			outputStream.write(bytes);
			outputStream.flush();
			outputStream.close();
			System.out.println(" Download Successful!");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
5.运行服务的FileDownServerPublisher类然后运行FileDownClient?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读