- 浏览: 21599 次
- 性别:
- 来自: 深圳
文章分类
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.");
}
}
}
相关推荐
- **配置文件**:如axis2.xml,这是Axis2的主配置文件,定义了服务的运行时行为。 2. **Axis2-1.6.2-war** 这是一个WAR(Web Application Archive)文件,适合在Servlet容器(如Tomcat、Jetty)中部署Axis2作为...
2. **bin版本**:"axis2-1.5.4-bin"通常包含Axis2运行时环境的全部文件,包括JAR库、配置文件、工具和其他必要的组件。这些文件可以用来快速搭建服务器环境,方便开发者在本地或者服务器上部署和运行Web服务。 3. *...
在描述中,我们看到"axis2-1.7.4-war.zip war包",这表明我们正在处理的是Axis2的可部署Web应用程序版本,它可以被上传到任何支持Servlet 2.4或更高版本的Web服务器,如Tomcat、Jetty等,以提供Web服务。 标签 ...
这个压缩包文件包含了与Axis2相关的jar文件,这些文件对于开发和实现Web服务至关重要。 Axis2是基于Java的,它提供了丰富的功能,包括服务部署、服务版本控制、消息处理和事务管理等。其设计目标是提高性能和可...
5. 部署服务:将aar文件上传到Axis2服务器,通过修改services.xml配置服务。 客户端的生成和使用步骤: 1. 获取WSDL:可以从服务端获取WSDL文件。 2. 生成客户端代码:使用Axis2的wsdl2java工具,根据WSDL生成Java...
#### 五、Axis2下载与安装 - **下载地址**:[Apache Axis2官方页面](http://ws.apache.org/axis2/) - **推荐版本**:1.4.1 - **文件选择**: - axis2-1.4.1-bin.zip:包含所有必需的jar文件。 - axis2-1.4.1-war....
- 直接在管理界面上传服务的AAR(Axis2 Archive)文件。 - 使用命令行工具axis2-admin或Ant脚本。 - 在代码中动态部署服务。 3. **服务调用** 客户端可以使用各种方式调用Axis2服务,如使用Java客户端API、SOAP...
本篇文章将深入探讨如何使用Axis2来实现文件的上传和删除功能,这对于构建分布式系统和跨平台应用至关重要。 首先,我们需要了解Axis2的基本概念。Axis2 是一个Web服务引擎,它支持SOAP 1.1和1.2以及RESTful风格的...
在本场景中,我们关注的是"Axis2上传接口",这是一个使用Java技术实现的功能,允许用户通过Web服务接口上传文件。下面我们将深入探讨Axis2上传接口的相关知识点。 1. **Axis2基础**: - Axis2是基于SOAP(Simple ...
- **二进制文件传输**:支持图片、文档等二进制数据类型的上传与下载,适用于文件共享或多媒体应用。 - **会话管理(Session Management)**:提供了基于HTTP Session的机制来跟踪客户端的交互过程,确保服务端能够...
4. **服务部署**: 创建完服务后,Wizard能帮助用户直接将服务打包成.AAR文件,然后通过Eclipse的部署工具将其上传到本地或者远程的Axis2服务器上,实现快速部署。 二、使用步骤 1. **安装插件**: 首先,你需要在...
本实战案例将关注使用Axis2进行复杂对象传输以及文件传输,特别是如何通过Web服务来传递列表(List)、对象(Object)和文件。 首先,让我们深入理解`axis2 Demo`。Axis2 提供了一个强大的开发和部署环境,它支持多种...
将 AAR 文件上传到 `http://localhost:8080/axis2/services/` 路径下,或者通过 Axis2 管理界面进行部署。 ### 第三部分:创建客户端 1. **生成 Stub 代码** - 使用 "Axis2 WSDL to Java Code Generator" 插件,...
- 通过Web管理界面,如Axis2管理控制台,上传AAR文件并发布服务。 5. Eclipse集成Axis2: - 安装Axis2插件:在Eclipse Marketplace中搜索并安装Axis2插件,如"Axis2 Web Service Developer Tools"。 - 创建Web...
- 部署服务时,右键点击项目,选择“Export”->"Axis2 Service Archive",然后按照向导操作,生成AAR文件并上传至Axis2服务器。 3. **Axis2的关键特性**: - **模块化架构**:Axis2允许将服务分解为可重用的模块...
- 将`.aar`文件上传到Tomcat中的Axis2部署目录(通常是`TOMCAT_HOME/webapps/axis2/WEB-INF/services`),Tomcat会自动识别并部署服务。 5. **测试Web服务**: - 部署完成后,可以在Axis2管理界面中看到新部署的...
然后,如果你打算将服务部署到生产环境,可以使用`axis2-1.6.2-war.zip`将其打包成一个WAR文件,并上传到你的Web服务器。同时,参考`WebService大讲堂之Axis2.pdf`可以深入理解Axis2的工作原理和最佳实践,提高开发...
【标题】"MYECLIPSE AXIS2 + SPRING 文件上传"所涉及的知识点主要集中在三个核心领域:MYECLIPSE开发环境、AXIS2服务框架和SPRING框架,以及文件上传技术。MYECLIPSE是一款强大的集成开发环境(IDE),常用于Java...
下载axis2-1.4.1-bin.zip和axis2-1.4.1-war.zip两个压缩包,前者包含所有必要的JAR文件,后者用于部署到Web容器中。将axis2.war文件部署到Tomcat的webapps目录下,启动Tomcat后,通过访问...
在本文中,我们将深入探讨Axis2在文件上传功能中的应用。 文件上传是Web应用程序中常见的功能,允许用户将本地文件发送到服务器进行处理或存储。在Axis2中实现文件上传,开发者可以利用HTTP协议的多部分/表单数据...