我正在开发一个项目,该项目在localhost中运行webservice以在远程计算机上上传文件。我正在使用jax-ws。当我的发件人类与服务器在同一个项目中时,它工作正常。但现在我正在尝试为发件人创建单独的项目,以便在远程计算机上上传文件,但我收到错误 -
javax.xml.ws.WebServiceException: Undefined port type: {http://fileUpload.cloud.com/}FileUploadService
at com.sun.xml.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:359)
我的代码是 -
public class CloudSender {
private static CloudProperties prop = new CloudProperties();
public static void send(String fileName, byte[] checkSum) throws MalformedURLException{
Properties connectionProp = prop.getPropertyObject();
URL url = new URL("http://localhost:8080/CloudSenderReceiver/FileUploadService?wsdl");
//URL url = new URL(connectionProp.getProperty("URL"));
QName qname = new QName("http://receiver.cloud.com/", "FileUploadService");
//QName qname = new QName(connectionProp.getProperty("NAMESPACE_URI"), connectionProp.getProperty("LOCALPART"));
Service service = Service.create(url, qname);
FileUploadService port = service.getPort(FileUploadService.class);
// enable MTOM in client
BindingProvider bp = (BindingProvider) port;
SOAPBinding binding = (SOAPBinding) bp.getBinding();
binding.setMTOMEnabled(true);
FileUploader f = new FileUploader();
DataSource source = new FileDataSource(new File(fileName));
DataHandler dh = new DataHandler(source);
Map<String, Object> ctxt = ((BindingProvider) port).getRequestContext();
ctxt.put(JAXWSProperties.HTTP_CLIENT_STREAMING_CHUNK_SIZE, 1024);
f.setFile(dh);
f.setFileType(FileUtils.getFileExtention(fileName));
f.setName(FileUtils.getFileName(fileName));
f.setChecksum(checkSum);
port.uploadFile(f);
}
}
界面 -
@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
@BindingType(value="http://schemas.xmlsoap.org/wsdl/soap/http?mtom=true")
public interface FileUploadService {
@WebMethod
void uploadFile(FileUploader file);
}
接口的实现 -
@MTOM
@WebService(portName = "fileUploadPort", endpointInterface = "com.cloud.receiver.FileUploadService", serviceName = "FileUploadService")
public class FileUploadServiceImpl implements FileUploadService {
private static CloudProperties prop = new CloudProperties();
@Override
public void uploadFile(FileUploader Dfile) {
Properties connectionProp = prop.getPropertyObject();
InputStream is = null;
OutputStream os = null;
String zipFilePath = "D:\\CloudTest\\UploadDataToCloud\\" + Dfile.getName() + "." + Dfile.getFileType();
//String zipFilePath = connectionProp.getProperty("ZIP_FILE_LOCATION_AT_CLOUD")+"\\" + Dfile.getName() + "." + Dfile.getFileType();
try {
DataHandler handler = Dfile.getFile();
File tempFolder = new File("D:\\CloudTest\\UploadDataToCloud");
//File tempFolder = new File(connectionProp.getProperty("ZIP_FILE_LOCATION_AT_CLOUD"));
if (!tempFolder.exists()) {
tempFolder.mkdir();
}
is = handler.getInputStream();
os = new FileOutputStream(new File(zipFilePath));
byte[] b = new byte[100000];
int bytesRead = 0;
while ((bytesRead = is.read(b)) != -1) {
os.write(b, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
if (os != null) {
os.flush();
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
我在Google上搜索并在
找到了相同的问题jax-ws Undefined port type with client and server in separate projects
,解决方案为
实现类中的@WebService注释中的endpointIneterface,如果你不提供端点接口,你必须在使用getPort方法时提到完全限定的端口名。
MyWSInterface dsws = service.getPort(PortQName,MyWSInterface.class);
我无法弄清楚,什么是完全合格的端口名称,在我的情况下,什么是完全合格的端口名称。