- 浏览: 20587 次
- 性别:
- 来自: 深圳
文章分类
service:
package com.siven.axis2.service;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Iterator;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMText;
import org.apache.axis2.AxisFault;
public class FileTransferServer
{
public static final String TMP_PATH = "C://temp//";
public OMElement upload(OMElement element) throws Exception
{
OMElement _fileContent = null;// 文件内容
OMElement _fileName = null;// 文件名
OMElement _fileType = null;// 文件类型
System.out.println("调用上传..");
// System.out.println("The element for upload: " + element);
for (Iterator _iterator = element.getChildElements(); _iterator.hasNext();)
{
OMElement _ele = (OMElement) _iterator.next();
if (_ele.getLocalName().equalsIgnoreCase("fileContent"))
{
_fileContent = _ele;
}
if (_ele.getLocalName().equalsIgnoreCase("fileName"))
{
_fileName = _ele;
}
if (_ele.getLocalName().equalsIgnoreCase("fileType"))
{
_fileType = _ele;
}
}
if (_fileContent == null || _fileType == null)
{
throw new AxisFault("Either Image or FileName is null");
}
OMText binaryNode = (OMText) _fileContent.getFirstOMChild();
String fileName = _fileName.getText();
String fileType = _fileType.getText();
String storeDir = TMP_PATH;
File dir = new File(storeDir);
if (!dir.exists())
{
dir.mkdir();
}
String filePath = storeDir + "/" + fileName + "." + fileType;
File uploadFile = new File(filePath);
if (uploadFile.exists())
{
filePath = storeDir + "/" + fileName + "(1)" + "." + fileType;
uploadFile = new File(filePath);
}
// Extracting the data and saving
DataHandler actualDH;
actualDH = (DataHandler) binaryNode.getDataHandler();
FileOutputStream imageOutStream = new FileOutputStream(uploadFile);
actualDH.writeTo(imageOutStream);
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace ns = fac.createOMNamespace("http://siven.org", "fileUpload");
OMElement ele = fac.createOMElement("response", ns);
ele.setText("true");
return ele;
}
public OMElement download(OMElement element) throws Exception
{
System.out.println("调用下载..");
// System.out.println("The element for download: " + element);
OMElement _userName = null;
OMElement _fileName = null;
OMElement _fileType = null;
for (Iterator _iterator = element.getChildElements(); _iterator.hasNext();)
{
OMElement _ele = (OMElement) _iterator.next();
if (_ele.getLocalName().equalsIgnoreCase("userName"))
{
_userName = _ele;
}
if (_ele.getLocalName().equalsIgnoreCase("fileName"))
{
_fileName = _ele;
}
if (_ele.getLocalName().equalsIgnoreCase("fileType"))
{
_fileType = _ele;
}
}
String userName = _userName.getText();
String fileName = _fileName.getText();
String fileType = _fileType.getText();
// String filePath = TMP_PATH + "/" + userName + "/" + fileName + "." + fileType;
String filePath = TMP_PATH + fileName + "." + fileType;
System.out.println("The filePath for download: " + filePath);
FileDataSource dataSource = new FileDataSource(filePath);
DataHandler expectedDH = new DataHandler(dataSource);
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace ns = fac.createOMNamespace("http://siven.org", "fileDownload");
OMText textData = fac.createOMText(expectedDH, true);
OMElement ele = fac.createOMElement("response", ns);
ele.addChild(textData);
return ele;
}
}
========================================================================================================
client:
package com.siven.axis2.client;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import org.apache.axiom.attachments.utils.IOUtils;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMText;
import org.apache.axiom.soap.SOAP11Constants;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
public class FileTransferClient
{
private static EndpointReference targetEPR = new EndpointReference("http://localhost:8888/Axis2/services/FileTransferServer");
public static boolean upload(String fileName, File file, String fileType)
{
try
{
OMElement data = buildUploadEnvelope(fileName, file, fileType);
Options options = buildOptions();
ServiceClient sender = new ServiceClient();
sender.setOptions(options);
System.out.println("The data in method upload: " + data);
OMElement ome = sender.sendReceive(data);
System.out.println("Convert the data to element in method upload: " + ome);
String b = ome.getText();
return Boolean.parseBoolean(b);
}
catch (Exception e)
{
e.printStackTrace();
}
return false;
}
public static boolean download(String userName, String fileName, String fileType)
{
try
{
OMElement data = buildDownloadEnvelope(userName, fileName, fileType);
Options options = buildOptions();
ServiceClient sender = new ServiceClient();
sender.setOptions(options);
System.out.println("The data in method download: " + data);
OMElement ome = sender.sendReceive(data);
System.out.println("Convert the data to element in method download: " + ome);
OMText binaryNode = (OMText) ome.getFirstOMChild();
binaryNode.setOptimize(true); // 必须加此句,否则会出现ContentID is null的异常!
DataHandler actualDH = (DataHandler) binaryNode.getDataHandler();
FileOutputStream imageOutStream = new FileOutputStream("c:/temp/3.png");
InputStream is = actualDH.getInputStream();
imageOutStream.write(IOUtils.getStreamAsByteArray(is));
return true;
}
catch (Exception e)
{
e.printStackTrace();
}
return false;
}
private static OMElement buildUploadEnvelope(String fileName, File file, String fileType)
{
DataHandler expectedDH;
OMFactory fac = OMAbstractFactory.getOMFactory();
//http://siven.org fileUpload 与服务端一致
OMNamespace omNs = fac.createOMNamespace("http://siven.org", "fileUpload");
OMElement data = fac.createOMElement("upload", omNs);
OMElement fileContent = fac.createOMElement("fileContent", omNs);
FileDataSource dataSource = new FileDataSource(file);
expectedDH = new DataHandler(dataSource);
OMText textData = fac.createOMText(expectedDH, true);
fileContent.addChild(textData);
OMElement _fileName = fac.createOMElement("fileName", omNs);
_fileName.setText(fileName);
OMElement _fileType = fac.createOMElement("fileType", omNs);
_fileType.setText(fileType);
data.addChild(_fileName);
data.addChild(_fileType);
data.addChild(fileContent);
return data;
}
private static OMElement buildDownloadEnvelope(String userName, String fileName, String fileType)
{
OMFactory fac = OMAbstractFactory.getOMFactory();
//http://siven.org fileDownload 与服务端一致
OMNamespace omNs = fac.createOMNamespace("http://siven.org", "fileDownload");
OMElement data = fac.createOMElement("download", omNs);
OMElement _userName = fac.createOMElement("userName", omNs);
_userName.setText(userName);
OMElement _fileName = fac.createOMElement("fileName", omNs);
_fileName.setText(fileName);
OMElement _fileType = fac.createOMElement("fileType", omNs);
_fileType.setText(fileType);
data.addChild(_userName);
data.addChild(_fileName);
data.addChild(_fileType);
return data;
}
private static Options buildOptions() throws AxisFault
{
Options options = new Options();
options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
options.setTo(targetEPR);
// enabling MTOM in the client side
options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
return options;
}
public static void main(String agrs[])
{
String uploadFile = "c:/1.png";//要上传的文件
String fileName = "2";//上传至服务器后的文件名
String fileType = "png";//上传至服务器后的文件后缀
boolean rtv = upload(fileName, new File(uploadFile), fileType);
System.out.println("is upload success: " + rtv);
String userName = "siven";
String downfileName = "2";//要下载的文件名 fileType为要下载的文件后缀
if (download(userName, downfileName, fileType))
{
System.out.println("download success.");
}
else
{
System.out.println("download fail.");
}
}
}
package com.siven.axis2.service;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Iterator;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMText;
import org.apache.axis2.AxisFault;
public class FileTransferServer
{
public static final String TMP_PATH = "C://temp//";
public OMElement upload(OMElement element) throws Exception
{
OMElement _fileContent = null;// 文件内容
OMElement _fileName = null;// 文件名
OMElement _fileType = null;// 文件类型
System.out.println("调用上传..");
// System.out.println("The element for upload: " + element);
for (Iterator _iterator = element.getChildElements(); _iterator.hasNext();)
{
OMElement _ele = (OMElement) _iterator.next();
if (_ele.getLocalName().equalsIgnoreCase("fileContent"))
{
_fileContent = _ele;
}
if (_ele.getLocalName().equalsIgnoreCase("fileName"))
{
_fileName = _ele;
}
if (_ele.getLocalName().equalsIgnoreCase("fileType"))
{
_fileType = _ele;
}
}
if (_fileContent == null || _fileType == null)
{
throw new AxisFault("Either Image or FileName is null");
}
OMText binaryNode = (OMText) _fileContent.getFirstOMChild();
String fileName = _fileName.getText();
String fileType = _fileType.getText();
String storeDir = TMP_PATH;
File dir = new File(storeDir);
if (!dir.exists())
{
dir.mkdir();
}
String filePath = storeDir + "/" + fileName + "." + fileType;
File uploadFile = new File(filePath);
if (uploadFile.exists())
{
filePath = storeDir + "/" + fileName + "(1)" + "." + fileType;
uploadFile = new File(filePath);
}
// Extracting the data and saving
DataHandler actualDH;
actualDH = (DataHandler) binaryNode.getDataHandler();
FileOutputStream imageOutStream = new FileOutputStream(uploadFile);
actualDH.writeTo(imageOutStream);
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace ns = fac.createOMNamespace("http://siven.org", "fileUpload");
OMElement ele = fac.createOMElement("response", ns);
ele.setText("true");
return ele;
}
public OMElement download(OMElement element) throws Exception
{
System.out.println("调用下载..");
// System.out.println("The element for download: " + element);
OMElement _userName = null;
OMElement _fileName = null;
OMElement _fileType = null;
for (Iterator _iterator = element.getChildElements(); _iterator.hasNext();)
{
OMElement _ele = (OMElement) _iterator.next();
if (_ele.getLocalName().equalsIgnoreCase("userName"))
{
_userName = _ele;
}
if (_ele.getLocalName().equalsIgnoreCase("fileName"))
{
_fileName = _ele;
}
if (_ele.getLocalName().equalsIgnoreCase("fileType"))
{
_fileType = _ele;
}
}
String userName = _userName.getText();
String fileName = _fileName.getText();
String fileType = _fileType.getText();
// String filePath = TMP_PATH + "/" + userName + "/" + fileName + "." + fileType;
String filePath = TMP_PATH + fileName + "." + fileType;
System.out.println("The filePath for download: " + filePath);
FileDataSource dataSource = new FileDataSource(filePath);
DataHandler expectedDH = new DataHandler(dataSource);
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace ns = fac.createOMNamespace("http://siven.org", "fileDownload");
OMText textData = fac.createOMText(expectedDH, true);
OMElement ele = fac.createOMElement("response", ns);
ele.addChild(textData);
return ele;
}
}
========================================================================================================
client:
package com.siven.axis2.client;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import org.apache.axiom.attachments.utils.IOUtils;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMText;
import org.apache.axiom.soap.SOAP11Constants;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
public class FileTransferClient
{
private static EndpointReference targetEPR = new EndpointReference("http://localhost:8888/Axis2/services/FileTransferServer");
public static boolean upload(String fileName, File file, String fileType)
{
try
{
OMElement data = buildUploadEnvelope(fileName, file, fileType);
Options options = buildOptions();
ServiceClient sender = new ServiceClient();
sender.setOptions(options);
System.out.println("The data in method upload: " + data);
OMElement ome = sender.sendReceive(data);
System.out.println("Convert the data to element in method upload: " + ome);
String b = ome.getText();
return Boolean.parseBoolean(b);
}
catch (Exception e)
{
e.printStackTrace();
}
return false;
}
public static boolean download(String userName, String fileName, String fileType)
{
try
{
OMElement data = buildDownloadEnvelope(userName, fileName, fileType);
Options options = buildOptions();
ServiceClient sender = new ServiceClient();
sender.setOptions(options);
System.out.println("The data in method download: " + data);
OMElement ome = sender.sendReceive(data);
System.out.println("Convert the data to element in method download: " + ome);
OMText binaryNode = (OMText) ome.getFirstOMChild();
binaryNode.setOptimize(true); // 必须加此句,否则会出现ContentID is null的异常!
DataHandler actualDH = (DataHandler) binaryNode.getDataHandler();
FileOutputStream imageOutStream = new FileOutputStream("c:/temp/3.png");
InputStream is = actualDH.getInputStream();
imageOutStream.write(IOUtils.getStreamAsByteArray(is));
return true;
}
catch (Exception e)
{
e.printStackTrace();
}
return false;
}
private static OMElement buildUploadEnvelope(String fileName, File file, String fileType)
{
DataHandler expectedDH;
OMFactory fac = OMAbstractFactory.getOMFactory();
//http://siven.org fileUpload 与服务端一致
OMNamespace omNs = fac.createOMNamespace("http://siven.org", "fileUpload");
OMElement data = fac.createOMElement("upload", omNs);
OMElement fileContent = fac.createOMElement("fileContent", omNs);
FileDataSource dataSource = new FileDataSource(file);
expectedDH = new DataHandler(dataSource);
OMText textData = fac.createOMText(expectedDH, true);
fileContent.addChild(textData);
OMElement _fileName = fac.createOMElement("fileName", omNs);
_fileName.setText(fileName);
OMElement _fileType = fac.createOMElement("fileType", omNs);
_fileType.setText(fileType);
data.addChild(_fileName);
data.addChild(_fileType);
data.addChild(fileContent);
return data;
}
private static OMElement buildDownloadEnvelope(String userName, String fileName, String fileType)
{
OMFactory fac = OMAbstractFactory.getOMFactory();
//http://siven.org fileDownload 与服务端一致
OMNamespace omNs = fac.createOMNamespace("http://siven.org", "fileDownload");
OMElement data = fac.createOMElement("download", omNs);
OMElement _userName = fac.createOMElement("userName", omNs);
_userName.setText(userName);
OMElement _fileName = fac.createOMElement("fileName", omNs);
_fileName.setText(fileName);
OMElement _fileType = fac.createOMElement("fileType", omNs);
_fileType.setText(fileType);
data.addChild(_userName);
data.addChild(_fileName);
data.addChild(_fileType);
return data;
}
private static Options buildOptions() throws AxisFault
{
Options options = new Options();
options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
options.setTo(targetEPR);
// enabling MTOM in the client side
options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
return options;
}
public static void main(String agrs[])
{
String uploadFile = "c:/1.png";//要上传的文件
String fileName = "2";//上传至服务器后的文件名
String fileType = "png";//上传至服务器后的文件后缀
boolean rtv = upload(fileName, new File(uploadFile), fileType);
System.out.println("is upload success: " + rtv);
String userName = "siven";
String downfileName = "2";//要下载的文件名 fileType为要下载的文件后缀
if (download(userName, downfileName, fileType))
{
System.out.println("download success.");
}
else
{
System.out.println("download fail.");
}
}
}
相关推荐
webservice相关axis2以及文件上传下载的jar,完成webservice的开发
axis2 webservice实现文件上传删除功能,只能用炫酷来形容,感兴趣就下载看看吧
Axis2快速发布服务详解:包括服务端与客户端。 此资源只包括脚本以及说明,整个目录没有上传。 我在此说明一下:根目录Axis2_141|---client |---server |---axis2-1.4.1(axis2的完整包) |---axis2(axis2_war...
AXIS2 + SPRING 文件上传 用MYECLIPSE直接导入文件运行即可.
----------示例包括: 普通数据类型 ...使用axis2通过自编写的server端生成wsdl,通过wsdl生成服务(aar,将aar文件放入tomcat/webapps/axis2/WEB-INF/services下),再通过wsdl生成客户端,通过客户端调用服务
由于上传不能超过20M,AxisTest里面的包需要朋友自己去下载,网上找axis2-1.5.1-bin.zip这个文件,下载然后解压,拷贝lib下面的所有包,加入到项目的lib目录下就OK,然后发布到tomcat上,然后进行java 客户端的测试...
基于 Apache Axis2 将上述类的方法对外提供 Web 服务,生成 WSDL 文件,以及调用 服务 2019-12-23根据全网的教程搭建最详细的教程 源码太大上传不了,但是教程内容十分详细,跟着我的教程一步一步就可以搭建起来,...
利用axis框架实现文件的上传和下载
文件上传功能的代码 博文链接:https://blueskylan.iteye.com/blog/253249
axis1 对象传输 多文件传输 javaBean传输 良心资源 学习axis1的学生少走弯路 ,传输复杂对象时 普通javaBean需要 序列化转换,传流文件时需要类型转换,如有报错可能是mail的jar与javaee的mail冲突。
Aixs2调用SharePoint Webservice实现文件上传下载删除等功能。
改代码使用eclipse编程,OrderClient类为调用示例,\interfaceImpl包中为发布的接口,因为链接的数据库,需要修改数据库链接
通过AXIS技术发布一个接口,供调用者上传图片等二进制数据
利用Isaac879的可用管脚和步骤文件,他的设计进行了修改,以实现聚焦和缩放控制。 在通读存储库的其余部分之前,强烈建议您查看在该项目上的原始工作。 在查看YouTube上的最终产品!目录1.2.3.设置安装Arduino IDE ...
初学axis2,这是我的第一个项目,比较简单,希望对大家有帮助。由于jar包太大,我没有权限上传,所以只截了张图在项目里。包里有struts2的包,放在里面没关系。里面默认的数据库是oracle,大家可以根据自己的情况换...
commons-fileupload1.3.3,commons-io2.6,axis2,axis,commons-collections3和4版本,jaxws.api.jar包
前段日子在这个上面搜索api,发现很多编译为chm格式的api似乎打开不了.上传一份html格式的文件.
的文件如下mat文件好像不是论坛上传的允许文件,所以我就打包了下。。: 新建文件夹.zip 对照doc pdepe里面的描述,解pde的主程序m文件如下:function pdex1 clear; load myproject.mat m = ...
的文件如下mat文件好像不是论坛上传的允许文件,所以我就打包了下。。: 新建文件夹.zip 对照doc pdepe里面的描述,解pde的主程序m文件如下:function pdex1 clear; load myproject.mat m = ...