您的位置:首页 > 其它

WebService 实现文件的上传下载(非自动生成)

2016-07-27 14:57 579 查看
    因公司新项目决定使用webservice与其它项目做交互,于是开始了webservice之旅。

    初入webservice的时候第一个接触的工具叫axis2,网上有着大量的简单案例。功能很强大,代码自动生成,能传递各种类型的数据。但是考虑到整合入公司项目的问题,决定去找一个不是自动生成手写的webservice。于是看到了关于JWS相关的文章,但是在使用JWS(java jdk自带)的时候难以实现对复杂参数的传递。于是想将文件读入后将其byte数组转换成String。以String的方式来传递文件,但是后来在测试的时候发现new String(byte[])的方式转换成的zip文件的String与webservice生成的xml相冲突。于是想到了将文件转换成base64字符串试试,于是有了如下代码:

server端:

package com.forms.tool.webservice;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.jws.WebService;

import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;

@WebService
public class WebServiceImp
{
public String sendFile(String name,String file) throws Exception
{
FileOutputStream fos = null;
try {
fos = new FileOutputStream("D:\\webserviceCBRC\\"+name);
byte[] filebs = new BASE64Decoder().decodeBuffer(file);
fos.write(filebs);
} catch (Exception e) {
e.printStackTrace();
} finally {
fos.close();
}
System.out.println("----------------------------------------------------------------------------------------");
System.out.println("----------------------------------------------------------------------------------------");
System.out.println("the file "+name+" was gotten !!");
System.out.println("----------------------------------------------------------------------------------------");
System.out.println("----------------------------------------------------------------------------------------");

File loc_file = new File("D:\\webserviceOT\\A001B0000001N000000000000000000002.zip");
FileInputStream fis = null;
String out = null;
try {
fis = new  FileInputStream(loc_file);
byte[] bs = new byte[(int)loc_file.length()];
fis.read(bs);
out = new BASE64Encoder().encode(bs);
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}

System.out.println("----------------------------------------------------------------------------------------");
System.out.println("----------------------------------------------------------------------------------------");

System.out.println("return CBRC");
System.out.println("----------------------------------------------------------------------------------------");
System.out.println("----------------------------------------------------------------------------------------");

return out;
}

}


发布:

package com.forms.tool.webservice;

import java.net
4000
.InetAddress;
import com.forms.tool.webservice.WebServiceImp;
import javax.xml.ws.Endpoint;

public class StartService
{
public static void main(String[] args)
{
try {
//获取当前IP
String ip = InetAddress.getLocalHost().getHostAddress();
//将服务发布到指定路径
Endpoint.publish("http://"+ip+":9527/webservice/CBRC", new WebServiceImp());
System.out.println("webservice 发布成功!");
} catch (Exception e) {
System.out.println("webservice 发布失败!");
;
}
}
}


客户端:

package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.URL;

import javax.xml.namespace.QName;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;

public class CBRC
{
public static void main(String[] args) throws Exception {

String endpoint = "http://192.168.100.17:9527/webservice/CBRC?wsdl";		//此处为wsdl地址
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new URL(endpoint));
//setOperationName 方法 Qname 前一个参数为设置namespace,后一个参数为设置想要访问的方法
call.setOperationName(new QName("http://webservice.tool.forms.com/","sendFile"));
//addParameter 方法即为添加元素的方法
call.addParameter("arg0",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);
call.addParameter("arg1",org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
//设置返回值类型
call.setReturnType(XMLType.XSD_STRING);

File xml1 = new File("C:\\Users\\admin\\Desktop\\webservice\\axis2\\A001B0000001N000000000000000000001.zip");
FileInputStream fis1 = new FileInputStream(xml1);

byte[] bytes1 = new byte[(int)xml1.length()];

fis1.read(bytes1);

//将byte数组转换为base64字符串
String base64file = new BASE64Encoder().encode(bytes1);

fis1.close();
//访问目标方法
String result = (String) call.invoke(new Object[]{"A001B0000001N000000000000000000001.zip",base64file});

FileOutputStream fos = new  FileOutputStream("C:\\Users\\admin\\Desktop\\webservice\\axis2\\A001B0000001N000000000000000000002.zip");
fos.write(new BASE64Decoder().decodeBuffer(result));
fos.close();

System.out.println("end");
}
}


关于使用的jar包

server端只需要:

    


client端需要的jar:

   




以及以下的部分。。。。。。。。。。



       


  

另外以上代码是查阅了大量网上代码后综合所得。就没有一一写引用出处

源码和axis2 jar包下载地址:http://download.csdn.net/detail/kokoyuo/9587652
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐