WebService文件传输
一、服务器端:
新建工程,servicetest:
新建类文件,UploadFileService:
代码:
代码:
st;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import javax.activation.DataHandler;
/**
* <b>function:</b>Axis WebService完成文件上传服务器端
*
*/
public class UploadFileService {
/**
* <b>function:</b>传递文件
*
* @param handler
* DataHandler这个参数必须
* @param fileName
* 文件名称
* @return upload Info
*/
public String upload(DataHandler handler, String fileName) {
if (fileName != null && !"".equals(fileName)) {
File file = new File(fileName);
if (handler != null) {
InputStream is = null;
FileOutputStream fos = null;
try {
is = InputStream();
fos = new FileOutputStream(file);
byte[] buff = new byte[1024 * 8];
int len = 0;
while ((len = is.read(buff)) > 0) {
fos.write(buff, 0, len);
}
} catch (FileNotFoundException e) {
return "fileNotFound";
} catch (Exception e) {
return "upload File failure";
} finally {
try {
if (fos != null) {
fos.flush();
fos.close();
}
if (is != null) {
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return "file absolute path:" + AbsolutePath();
} else {
return "handler is null";
}
} else {
return "fileName is null";
}
}
}
基于该实现类,创建 Web Service,如下图:
点击Next会出现,可以忽略这个警告。
点击Next会出现,可以忽略这个警告。
会自动生成这些文件:
启动tomcat时,生成server-config.wsdd文件。
启动信息里面有:警告: Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
解决方法:将mail.jar和activation.jar放到lib目录下(我都是在www.jar114上到的)。
解决方法:将mail.jar和activation.jar放到lib目录下(我都是在www.jar114上到的)。
二、客户端:
1.编写类UploadFileClient,代码:
st;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
l.namespace.QName;
l.rpc.Call;
l.rpc.ParameterMode;
l.rpc.Service;
l.rpc.ServiceFactory;
import org.ding.XMLType;
public class UploadFileClient {
/**
* @param args
*/
public static void main(String[] args) {
String address = "localhost:8080/servicetest/services/UploadFileService";
String namespaceURI = "hello";
String serviceName = "UploadFileServiceService";
String portName = "UploadFileService";
String operationName = "upload";
try {
ServiceFactory factory = wInstance();
Service service = ateService(new QName(serviceName));
Call call = ateCall(new QName(portName));
call.setTargetEndpointAddress(address);
QName sqn = new QName("fileName", "String");
String filepath = "d://test.zip";
DataHandler handler = new DataHandler(new FileDataSource(filepath));
QName hqn = new QName("handler", "DataHandler");
call.setOperationName(new QName(namespaceURI, operationName));
call.addParameter("handler", hqn, ParameterMode.IN);
call.addParameter("fileName", sqn, ParameterMode.IN);
调用webservice服务 call.setReturnType(XMLType.XSD_STRING);
Object[] inParams = new Object[2];
inParams[0] = handler;
inParams[1] = "remote_test.zip";
String value = call.invoke(inParams).toString();
System.out
.println("Dynamic Client Invocation:\n\tThe summation is: "
+ value);
} catch (Exception e) {
e.printStackTrace();
}
}
}
启动tomcat以后,运行UploadFileClient,文件传输就完成了
未解决的问题:
Tomcat6和Tomcat7在eclipse中启动的时候报错:
2011-7-12 13:05:59 org.apache.catalina.startup.SetContextPropertiesRule begin
警告: [SetContextPropertiesRule]{Context} Setting property 'source' to 'lipse.jst.j2ee.server:test1' did not find a matching property.
参考资料:
1.www.ibm/developerworks/cn/webservices/1106_webservicessecurity/?cmp=dwskl&cpb=dw&ct=dwcon&cr=cn_CCID&ccy=cn
2.wwwblogs/hoojo/archive/2010/12/23/1911385.html
3.topic.csdn/t/20060702/12/4855371.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论