Axis2上传下载文件
Service:
package com.siven.axis2.service;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import javax.activation.DataHandler;
public class FileService
{
// 使用byte[]类型参数上传二进制文件
public boolean uploadWithByte(byte[] file, String filename)
{
FileOutputStream fos = null;
try
{
fos = new FileOutputStream("c:/temp/"+filename);
fos.write(file);
fos.close();
}
catch (Exception e)
{
return false;
}
finally
{
if (fos != null)
{
try
{
fos.close();
}
catch (Exception e)
{
}
}
}
return true;
}
private void writeInputStreamToFile(InputStream is, OutputStream os) throws Exception
{
int n = 0;
byte[] buffer = new byte[8192];
while ((n = is.read(buffer)) > 0)
{
os.write(buffer, 0, n);
}
}
// 使用DataHandler类型参数上传文件
public boolean uploadWithDataHandler(DataHandler file, String filename)
{
System.out.println("====================== start");
FileOutputStream fos = null;
try
{
fos = new FileOutputStream("c:/temp/"+filename);
// 可通过DataHandler类的getInputStream方法读取上传数据
writeInputStreamToFile(file.getInputStream(), fos);
fos.close();
}
catch (Exception e)
{
return false;
}
finally
{
if (fos != null)
{
try
{
fos.close();
}
catch (Exception e)
{
}
}
}
System.out.println("====================== end");
return true;
}
public byte[] downWithByte(String fileName) throws Exception
{
// 打开图像文件,确定图像文件的大小
String filePath = "c:/temp/"+fileName;
File file = new File(filePath);
java.io.FileInputStream fis = new java.io.FileInputStream(filePath);
// 创建保存要上传的图像文件内容的字节数组
byte[] buffer = new byte[(int) file.length()];
// 将图像文件的内容读取buffer数组中
int n = fis.read(buffer); //n为读入的字节数
System.out.println("文件长度:" + file.length());
fis.close();
return buffer;
}
}
===================================================================================================
RPC Client:
public static void invokeUploadWithByte() throws AxisFault
{
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference("http://localhost:8888/Axis2/services/fileService");
options.setTo(targetEPR);
DataHandler dh = new DataHandler(new FileDataSource("c:/1.png"));//要上传的文件路径
Object[] opAddEntryArgs = new Object[]{ dh,"a.png" };//上传到服务器的文件名
Class[] classes = new Class[]{ Boolean.class };
QName opAddEntry = new QName("http://service.axis2.siven.com","uploadWithByte");
System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs,classes)[0]);
}
public static void invokeUploadWithDataHandler() throws AxisFault
{
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference("http://localhost:8888/Axis2/services/fileService");
options.setTo(targetEPR);
DataHandler dh = new DataHandler(new FileDataSource("c:/1.png"));//要上传的文件路径
Object[] opAddEntryArgs = new Object[]{ dh,"fun.png" }; //上传到服务器的文件名
Class[] classes = new Class[]{ Boolean.class };
QName opAddEntry = new QName("http://service.axis2.siven.com","uploadWithDataHandler");
System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs,classes)[0]);
}
public static void invokeDownWithByte() throws AxisFault
{
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference("http://localhost:8888/Axis2/services/fileService");
options.setTo(targetEPR);
Object[] opAddEntryArgs = new Object[] {"a.png"};//要下载的文件名
Class[] classes = new Class[] {byte[].class};
QName opAddEntry = new QName("http://service.axis2.siven.com","downWithByte");
byte[] strArray = (byte[])serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0];
FileOutputStream fos = null;
try
{
// 将下载的图像保存在c:/temp/盘的down.png文件中
fos = new FileOutputStream("c:/temp/down.png");
// 开始写入图像文件的字节
fos.write(strArray);
fos.close();
}catch(Exception e){
e.printStackTrace();
}
System.out.println("OK");
}
Stub Client:
public static void fileServiceTest() throws RemoteException
{
FileServiceStub fss = new FileServiceStub();
DataHandler dh = new DataHandler(new FileDataSource(new File("c:/1.png")));//要上传的文件路径
FileServiceStub.UploadWithByte uwb = new FileServiceStub.UploadWithByte();
uwb.setFile(dh);
uwb.setFilename("ccc.png");//上传到服务器的文件名
FileServiceStub.UploadWithByteResponse res =fss.uploadWithByte(uwb);
System.out.println(res.get_return());
FileServiceStub.UploadWithDataHandler uwh = new FileServiceStub.UploadWithDataHandler();
uwh.setFile(dh);
uwh.setFilename("ddd.png");//上传到服务器的文件名
FileServiceStub.UploadWithDataHandlerResponse resh = fss.uploadWithDataHandler(uwh);
System.out.println(resh.get_return());
}
分享到:
相关推荐
axis2-idea-plugin-1.7.9
axis2-eclipse-codegen-plugin-1.6.2.zip和axis2-eclipse-service-plugin-1.6.2.zip,解压即可使用
共四个文件,都是最先版的,希望可以帮助大家。axis2-eclipse-service-archiver-wizard和axis2-eclipse-codegen-wizard和axis2-1.6.1-bin和axis2-1.6.1-war
目前axis2最高版本是2.0以上的版本,但是eclipse和myeclipse都不支持,无奈只能使用低版本的插件1.6.3;经实验,可以安装成功; 安装方法:右键解压到当前文件夹,Copy解压的文件到eclipse安装目录dropins下,重启...
1、axis2相关jar包如下: axiom-api-1.2.10.jar axiom-dom-1.2.10.jar axiom-impl-1.2.10.jar axis2-adb-1.5.4.jar axis2-adb-codegen-1.5.4.jar axis2-codegen-1.5.4.jar axis2-corba-1.5.4.jar axis2-fastinfoset-...
axis2-std-1.0-bin.zip与axis2.war axis2-std-1.0-bin.zip与axis2.war
绝对可以用,里面配有myeclipse10安装AXIS2插件的步骤,希望大家工作愉快,谢谢,给好评哦
axis2-eclipse-service-archiver-wizard.zip
tools的工具包,axis2,apache-cxf
axis2-kernel-1.3.jar axis2-kernel-1.3.jar
axis2-eclipse-codegen-plugin-1.6.2+axis2-eclipse-service-plugin-1.6.2
axis2包含axis2-1.4.1-war.zip和axis2-1.4.1-bin.zip,希望大家喜欢。
axis2-eclipse-codegen-plugin-1.6.0和axis2-eclipse-service-plugin-1.6.0有关jar包下载,解压后直接将这两个jar包放到eclips下的plugins目录下,重启eclipse即可
axis2 webservice 服务端jar包: -->axis2-kernel-1.6.1.jar -->axis2-spring-1.6.1.jar -->axis2-transport-http-1.6.1.jar -->XmlSchema-1.4.7.jar -->wsdl4j-1.6.2.jar -->axiom-api-1.2.12.jar -->axiom...
axis2c-bin-1.6.0-linux.tar.gz axis2c-bin-1.6.0-win32.zip axis2c-src-1.6.0.tar.gz axis2c-src-1.6.0.zip 加md5
axis2-1.6.2-war、axis2-1.6.1-war、axis2-1.6.2-bin
axis2-1.5.3-bin.zip axis2-1.5.3-bin.zip axis2-1.5.3-bin.zip axis2-1.5.3-bin.zip
axis2-1.6.2.zip, windows axis2工具,根据 WSDL生成java文件。 1、axis2客户端下载地址:http://mirror.esocc.com/apache//axis/axis2/java/core/1.6.2/axis2-1.6.2-bin.zip; 2、下载解压在D:\Work_Program_...
axis2-1.6.0-bin和axis2-1.6.0-war相关资源
axis2-1.7.9-bin.zip axis2最新版本的工具包bin在这里了