WebService完成文件上传下载
由于开发需要使用webservice,第一个接触的工具叫axis2。项目开发相关jar下载。

service端:
启动类:
import java.net.InetAddress;
import javax.xml.ws.Endpoint; public class StartService
{
public static void main(String[] args)
{
try {
//获取当前IP
String ip = InetAddress.getLocalHost().getHostAddress();
//将服务发布到指定路径
System.out.println("IP:" + ip);
String relativelyPath=System.getProperty("user.dir");
System.out.println(relativelyPath);
Endpoint.publish("http://"+ip+":9527/webservice/CBRC", new WebServiceImp());
System.out.println("webservice 发布成功!");
} catch (Exception e) {
System.out.println("webservice 发布失败!");
;
}
}
}
实现类:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import javax.jws.WebService; import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder; //import Decoder.BASE64Decoder;
//import Decoder.BASE64Encoder; @WebService
public class WebServiceImp
{
public String sendFile(String name,String file) throws Exception
{
//上传文件
String preference_path = "/webserviceupload";
String relativelyPath=System.getProperty("user.dir");
//存储路径
String fileupload = relativelyPath + preference_path;
File filepath = new File(fileupload);
if (!filepath.exists()) {
filepath.mkdirs();
} /**
*生成上传文件
*/
FileOutputStream fos = null;
try {
fos = new FileOutputStream(fileupload + File.separator + 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("----------------------------------------------------------------------------------------"); int splitIndex = name.lastIndexOf(".");
String newName = name.substring(0,splitIndex) + ".pdf";
TransferToPDFUtil.PdfManager(fileupload +File.separator+ name, fileupload +File.separator+ newName);
System.out.println("----------------------------------------------------------------------------------------");
System.out.println("finish file transfer");
System.out.println("----------------------------------------------------------------------------------------"); /**
* 上传文件到客户端
*/
File loc_file = new File(fileupload +File.separator+ newName);
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;
} }
client端:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import javax.xml.namespace.QName; import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient; import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder; public class CBRC {
public static void main(String[] args) throws Exception { // 使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions(); EndpointReference targetEPR = new EndpointReference(
"http://10.74.3.191:9527/webservice/CBRC?wsdl");// 指定调用WebService的URL
options.setTo(targetEPR); // RPCServiceClient类的invokeBlocking方法调用了WebService中的方法。invokeBlocking方法有三个参数,其中第一个参数的类型是QName对象,表示要调用的方法名;第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}。 /*String endpoint = "http://10.74.3.191: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://wtp/","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\\lenovo\\Desktop\\1.doc");
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[]{"1.doc",base64file});
//指定方法返回值的数据类型的Class对象
Class[] classes = new Class[] { String.class };// 指定getGreeting方法返回值的数据类型的Class对象 QName opAddEntry = new QName("http://wtp/","sendFile");// 指定要调用的getGreeting方法及WSDL文件的命名空间
Object[] opAddEntryArgs = new Object[]{"1.doc",base64file};// 指定getGreeting方法的参数值
String result = serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0].toString();
/**
* 下载文件
*/
FileOutputStream fos = new FileOutputStream("C:\\Users\\lenovo\\Desktop\\1.pdf");
fos.write(new BASE64Decoder().decodeBuffer(result));
fos.close(); System.out.println("end");
}
}
原文引用:
其他参考:
WebService完成文件上传下载的更多相关文章
- iOS开发之结合asp.net webservice实现文件上传下载
iOS开发中会经常用到文件上传下载的功能,这篇文件将介绍一下使用asp.net webservice实现文件上传下载. 首先,让我们看下文件下载. 这里我们下载cnblogs上的一个zip文件.使用N ...
- WebService实现文件上传下载
一:服务端:一个普通java web工程 package com.wzh.file; import com.sun.xml.ws.developer.StreamingAttachment; impo ...
- webservice文件上传下载
使用DataHandler实现webservice的文件上传下载 服务端代码: package com.hello.weChat.controller; import javax.activation ...
- Struts的文件上传下载
Struts的文件上传下载 1.文件上传 Struts2的文件上传也是使用fileUpload的组件,这个组默认是集合在框架里面的.且是使用拦截器:<interceptor name=" ...
- Android okHttp网络请求之文件上传下载
前言: 前面介绍了基于okHttp的get.post基本使用(http://www.cnblogs.com/whoislcj/p/5526431.html),今天来实现一下基于okHttp的文件上传. ...
- Selenium2学习-039-WebUI自动化实战实例-文件上传下载
通常在 WebUI 自动化测试过程中必然会涉及到文件上传的自动化测试需求,而开发在进行相应的技术实现是不同的,粗略可划分为两类:input标签类(类型为file)和非input标签类(例如:div.a ...
- 艺萌文件上传下载及自动更新系统(基于networkComms开源TCP通信框架)
1.艺萌文件上传下载及自动更新系统,基于Winform技术,采用CS架构,开发工具为vs2010,.net2.0版本(可以很容易升级为3.5和4.0版本)开发语言c#. 本系统主要帮助客户学习基于TC ...
- 艺萌TCP文件上传下载及自动更新系统介绍(TCP文件传输)(一)
艺萌TCP文件上传下载及自动更新系统介绍(TCP文件传输) 该系统基于开源的networkComms通讯框架,此通讯框架以前是收费的,目前已经免费并开元,作者是英国的,开发时间5年多,框架很稳定. 项 ...
- ssh框架文件上传下载
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
随机推荐
- 内核顶层Makefile相关2
http://www.groad.net/bbs/simple/?f104.html if 函数 if 函数的语法有两种形式: () $(if <condition>, <then ...
- 编译3.10内核 出现错误 “undefined reference to...." 解决方法
向内核中加入C文件后.假设想编译进内核须要改动当前文件夹下的Kconfig文件和Makefile文件. 如:加入一个test.c文件到driver文件夹下,则须要改动Kconfig文件: config ...
- Sublime Text3 配置设置攻略
转载:http://cloudbbs.org/forum.php?mod=viewthread&tid=3620 sublime本身功能有限,我们需要装上一些插件使其变得强大.sublime在 ...
- Integrate NSX into Neutron
NSX is VMware's strategy for Software-defined networking, it was implemented purely in software, and ...
- ORACLE SQL性能优化(全)
ORACLE SQL性能优化(全) http://wenku.baidu.com/view/b2aaba3887c24028915fc337.html
- struts2的输入检验
一.输入校验简介 一个健壮的Web应用程序必须确保用户输入是合法的.比如在注册用户的时候,将用处注册信息保存到数据库之前一般我们会判断用户输入的密码长度是否过短,或者用户的email地址格式是否正确. ...
- 九度OJ 1092:Fibonacci (递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1923 解决:1378 题目描述: The Fibonacci Numbers{0,1,1,2,3,5,8,13,21,34,55...} ...
- mysql的事务隔离级别及其使用场景
1 什么是事务隔离级别 事务隔离指的是事务之间同步关系. 2 食物隔离级别的分类 第一隔离级别,脏读级别 在脏读级别下,第一个事务修改了某个数据,但是还没有提交,第二个事务可以读取到这个未提及的数据. ...
- LINUX上一个命令计算PI
Linux上一个命令计算PI – 笑遍世界 http://smilejay.com/2017/11/calculate-pi-with-linux-command/ [root@d1 goEcho]# ...
- ElasticSearch(一)什么是全文检索?
全文检索 全文检索,即倒排索引.