用的是axis2 1.4.1 在网上找的例子,上传 几十M 的没问题,但是上传800M的报错,不知可否告知一二
上代码:
service端:
package com.ws.files;
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 FileTransferService {
public static final String TMP_PATH="F://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 + "//" + "velmaUpload";
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://example.org/filedata",
"fd");
OMElement ele = fac.createOMElement("response", ns);
ele.setText("true");
return ele;
}
public OMElement download(OMElement element) throws Exception {
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;
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://example.org/filedata",
"fd");
OMText textData = fac.createOMText(expectedDH, true);
OMElement ele = fac.createOMElement("response", ns);
ele.addChild(textData);
return ele;
}
}
client端:
package com.ws.files;
/**
* FileTransferClient.java
* 版权所有(C) 2011 cuiran2001@163.com
* 创建:崔冉 Jan 4, 2011 10:09:47 AM
*/
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;
/**
* @author 崔冉
* @version 1.0.0
* @desc
*/
public class FileTransferClient {
static String IP = "192.168.1.8:8080";
static String targetEndpoint = "http://" + IP
+ "/fileService/services/fileTransferService";
private static EndpointReference targetEPR = new EndpointReference(
targetEndpoint);
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();
System.out.println(b);
// return Boolean.parseBoolean(b);
return true;
} 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//steal.xml");
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();
OMNamespace omNs = fac.createOMNamespace("http://files.ws.com",
"upload");
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();
OMNamespace omNs = fac.createOMNamespace("http://files.ws.com",
"upload");
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 file = "E:\\office.rar";
String fn = "office"; // 文件名称
String ft = "rar"; // 文件类型
boolean rtv = upload(fn, new File(file), ft);
System.out.println("is upload success: " + rtv);
// 下载
// boolean rtv = download("return","steal","xml");
// System.out.println("is download success: "+rtv);
}
}
上传200M的文件出现错误为:
客户端:
org.apache.axis2.AxisFault: Java heap space
at org.apache.axis2.util.Utils.getInboundFaultFromMessageContext(Utils.java:512)
at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:370)
at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:416)
at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:228)
at org.apache.axis2.client.OperationClient.execute(OperationClient.java:163)
at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:548)
at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:528)
at com.ws.files.FileTransferClient.upload(FileTransferClient.java:48)
at com.ws.files.FileTransferClient.main(FileTransferClient.java:145)
服务端:
严重: Servlet.service() for servlet Axis2Servlet threw exception
org.apache.axiom.om.OMException: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at org.apache.axiom.attachments.impl.PartFactory.createPart(PartFactory.java:127)
at org.apache.axiom.attachments.Attachments.getPart(Attachments.java:624)
at org.apache.axiom.attachments.Attachments.getNextPartDataHandler(Attachments.java:542)
at org.apache.axiom.attachments.Attachments.getContentIDSet(Attachments.java:487)
at org.apache.axiom.attachments.Attachments.getAllContentIDs(Attachments.java:478)
at org.apache.axis2.transport.TransportUtils.deleteAttachments(TransportUtils.java:486)
at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:202)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:617)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1774)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.AbstractStringBuilder.substring(AbstractStringBuilder.java:881)
at java.lang.StringBuffer.substring(StringBuffer.java:416)
at org.apache.axiom.attachments.impl.PartFactory.readHeader(PartFactory.java:232)
at org.apache.axiom.attachments.impl.PartFactory.readHeaders(PartFactory.java:199)
at org.apache.axiom.attachments.impl.PartFactory.createPart(PartFactory.java:85)
... 20 more
相关推荐
标题中的"axis2-1.4.1-bin.zip"和"axis2-1.4.1-war.zip"指的是Apache Axis2的两个不同版本的发行包,分别代表了Axis2的可执行二进制版本和Web应用程序版本。Apache Axis2是一个高度可扩展且功能强大的Web服务引擎,...
在"axis2-1.4.1-bin.zip"这个压缩包中,包含了Axis2框架的1.4.1版本的所有必要组件和资源,使得用户可以快速地搭建和运行Web服务。 **1. Axis2架构** Axis2的核心设计理念是模块化,它由多个模块组成,包括核心引擎...
1. **使用wsdl2java工具**:Axis2提供了一个名为wsdl2java的命令行工具,可以将WSDL文件转换为Java类。这些类包括服务接口(SEI, Service Endpoint Interface)和消息处理器(Skeleton和Stub),使得开发者可以直接...
在压缩包文件名称列表中,我们只有一个条目:“axis2-1.4.1-war”。这通常意味着压缩文件包含了完整的Axis2 1.4.1 Web服务引擎的Web应用版本,可能包括以下组件: 1. `WEB-INF`目录:这是Web应用的核心部分,包含`...
这个压缩包“axis2-1.4.1.rar”包含的是Axis2的1.4.1版本,这是一个重要的里程碑,因为它在当时提供了许多关键的功能改进和性能优化。 1. **Axis2简介**:Apache Axis2是基于Axis1的完全重写,设计目标是提高性能、...
2. **备份原有jar**:在应用补丁之前,建议备份原有的“axis1.4.jar”文件,以便在出现问题时可以回滚到之前的版本。 3. **替换jar**:将新生成的“axis1.4.1.jar”文件替换掉项目中现有的“axis1.4.jar”,确保补丁...
而“axis2-1.4.1”可能是一个包含所有Axis2库文件、示例和服务部署文件的目录,用户可以解压后在本地环境中运行和测试Web服务。 在使用Axis2时,开发者可以创建服务部署档案(.aar文件),这些档案包含了服务的类、...
在给定的压缩包文件中,我们有两个版本的Axis2:`axis2-1.4.1-war.zip` 和 `axis2-1.4.1-bin.zip`。这两个版本的主要区别在于它们的用途和部署方式。 1. `axis2-1.4.1-war.zip`: 这个版本是WAR(Web Application ...
创建一个新项目,将下载的Axis2_1.4.1中的`jar`文件、`modules`以及`axis2.xml`配置文件导入项目中,构建项目的目录结构。这一步是基础环境搭建的关键环节,直接影响到后续服务的正常运行。 ##### 步骤3:服务类与...
1):用POJO实现0配置的WebService 2):复合类型数据的传递 3):使用services.xml文件发布WebService 4):二进制文件传输 5):会话(Session)管理 6):跨服务会话(Session)管理 7):将Spring的装配JavaBean发布成 8...
4. 部署服务:使用Axis2插件将服务打包成aar(Axis2 Archive)文件,并上传到Axis2服务器。 5. 生成服务客户端:Eclipse插件可以自动生成客户端Stub代码,便于调用服务。 6. 运行和测试服务:在Eclipse内置的Tomcat...
axis2-adb-1.4.1.jar axis2-adb-1.4.1.jar
AXIS2 webservice apache 开源 axis2-1.4.1.war
官方版本,亲测可用
AXIS2 webservice apache 开源 axis2-1.4.1-bin.zip
标题 "axis2-1.4.1-war.zip" 指的是 Axis2 框架的一个特定版本,1.4.1,以 WAR(Web Application Archive)格式打包。WAR 文件是一种标准的 Java Web 应用程序打包方式,包含了运行一个 Web 应用所需的所有资源,如 ...