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 ...
随机推荐
- GIS+=地理信息+行业+大数据——纽约公开11亿条出租车和Uber原始数据下载及分析
一览众山小编辑团队 原文/ Todd Schneider 翻译/ 沈玮薇 陈翚 文献/ 蒋理 校核/ 众山小编辑/ 众山小 排版/ 徐颖 2014-2015 © 转载请注明:源自公众号"一览 ...
- Hdu-1565 方格取数(1) (状态压缩dp入门题
方格取数(1) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- Linaro/Yocto/Openwrt
http://en.wikipedia.org/wiki/Linaro Linaro From Wikipedia, the free encyclopedia This article ap ...
- centos7下MySQL的配置
1. 下载mysql的repo源 wget http:.noarch.rpm 2. 安装mysql-community-release-el7-5.noarch.rpm包 rpm .noarch.rp ...
- PHP网站加网站访问量统计
新建cnt.php <?php $n=file_get_contents('cnt.txt'); $n++; file_put_contents('cnt.txt',$n); echo &quo ...
- IP和归属地
ip: http://www.ip.cn/index.php?ip=10.132.98.143 归属地: http://www.ip138.com:8080/search.asp?action=mob ...
- javascript中提高代码的封装性
我出的面试题中,有一条是问如何避免页面引用JS,出现函数.变量重复.冲突的. 从大的方面讲,应该引入javascript的模块化开发,符合AMD规范之类: 从小的方面说,大概就是限定变量和函数的作用域 ...
- Transforming Auto-encoders
http://www.cs.toronto.edu/~hinton/absps/transauto6.pdf The artificial neural networks that are used ...
- WildFly JBoss 应用程序服务器
https://en.wikipedia.org/wiki/WildFly [实现基于面向服务的架构SOA的web应用和服务] WildFly,[1] formerly known as JBoss ...
- ElasticSearch(十一)批量CURD bulk
1.bulk语法 POST /_bulk { "delete": { "_index": "test_index", "_type ...