<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-frontend-jaxwsartifactId>
<version>3.1.9version>
dependency>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-transports-http-jettyartifactId>
<version>3.1.9version>
dependency>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-coreartifactId>
<version>3.1.9version>
dependency>
@WebService(endpointInterface = "com.ds.webServiceDemo.UserService",targetNamespace="http://cems.ds/userService")
public interface UserService {
/**
* 用户登录
* @param userAccount 账号
* @param password 密码
* @return
*/
public Result login(@WebParam(name = "userAccount")String userAccount, @WebParam(name = "password")String password);
/**
* 获取用户详细信息
* @param userId 用户ID
* @return
*/
public Result getUserDetalis(@WebParam(name = "userId")String userId);
}
@WebService
public class UserServiceImpl implements UserService {
@WebMethod
@Override
public Result login(String userAccount, String password) {
/**
* 进行业务处理
*/
return null;
}
@WebMethod
@Override
public Result getUserDetalis(String userId) {
/**
* 进行业务处理
*/
return null;
}
}
public class ServiceMain {
public static void main( String[] args ){
System.out.println("webService服务正在启动");
String serverIp = "127.0.0.1";//使用时,ip可以通过配置文件获取
String serverPort = "8080";//使用时,端口可以通过配置文件获取
String userServiceUrl = "http://"+serverIp+":"+serverPort+"/userService";
Endpoint.publish(userServiceUrl, new UserServiceImpl());
System.out.println("userService发布地址:" + userServiceUrl);
System.out.println("webService服务启动成功");
}
}
@WebService(endpointInterface = "com.ds.webServiceDemo.FileService",targetNamespace="http://cems.ds/fileService")
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface FileService {
/**
* 上传文件
* @param fileName 文件名
* @param file 文件信息,二进制文件
* @return
*/
public Result uploadFile(@WebParam(name = "fileName")String fileName, @WebParam(name = "file")DataHandler file);
/**
* 下载文件
* @param filePath 需要下载的文件路径,也可以是文件标识
* @return
*/
public DataHandler downloadFile(@WebParam(name = "filePath")String filePath)throws FileNotFoundException ;
}
@WebService
public class FileServiceImpl implements FileService {
@WebMethod
@Override
public Result uploadFile(String fileName, DataHandler handler) {
Result result = new Result();
//就可以将文件存放到本地了
File file = new File("文件存放路径" + fileName);
try {
FileUtils.copyInputStreamToFile(handler.getDataSource().getInputStream(), file);
result.setFlag(0);
result.setData("文件上传成功");
} catch (IOException e) {
e.printStackTrace();
result.setFlag(-1);
result.setData("文件上传出现异常");
}
return result;
}
@WebMethod
@Override
public DataHandler downloadFile(String filePath) throws FileNotFoundException {
File downloadFile = new File(filePath);
if (!downloadFile.exists()) {
System.out.println("需要下载的文件不存在");
throw new FileNotFoundException(filePath + " does not exist");
}
DataHandler dataHandler = new DataHandler(new FileDataSource(filePath));
return dataHandler;
}
}
public class ServiceMain {
public static void main( String[] args ){
System.out.println("webService服务正在启动");
String serverIp = "127.0.0.1";
String serverPort = "8080";
String userServiceUrl = "http://"+serverIp+":"+serverPort+"/userService";
String fileServiceUrl = "http://"+serverIp+":"+serverPort+"/fileService";
Endpoint.publish(userServiceUrl, new UserServiceImpl());
System.out.println("userService发布地址:" + userServiceUrl);
Endpoint.publish(fileServiceUrl, new FileServiceImpl());
System.out.println("fileService发布地址:" + fileServiceUrl);
System.out.println("webService服务启动成功");
}
}
可以从官网下载,也可以在我网盘中进行下载,安装使用时,电脑需要安装jdk
链接:https://pan.baidu.com/s/19bh1UNVH4Tfd7fDg8M83bA
提取码:jz63
打开SoapUI后,在Projects上鼠标右击,选择New SOAP Project
在Initial WSDL中输入发布的webService接口的wsdl文件访问URL,然后点击OK按钮
在fileName标签中输入上传的文件名,在file中,鼠标右击,选择Insert file as Base64,然后选择本地文件,点击打开即可完成文件的选择,点击Submit request to specified endpoint URL进行测试
查看源码