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 ...
随机推荐
- python(9)- python基础知识刷题
1. 执行 Python 脚本的两种方式 交互方式:命令行 Windows操作系统下,快捷键cmd,输入“python”启动交互式python解释器. 文件方式:python文件 2. 简述位.字 ...
- DM8168 unrecoverable error: OMX_ErrorBadParameter (0x80001005) [resolved]
DM8168 custom board 成功启动系统之后想先測一下8168编解码功能,把开发包里的examples跑一遍.启动完毕后.连上HDMI显示,在starting Matrix GUI app ...
- UVA11770 - Lighting Away
题目链接 题意:一个有向图,每对一个结点操作.就能够触发连锁反应,使得该结点及它直接或间接指向的点均获得标记,问至少须要操作多少个结点使得全部结点获得标记 思路:有向图的强连通分量.用Tarjan缩点 ...
- C#.NET开源项目、机器学习、Power BI (转载)
.NET技术, 开源项目, 数据挖掘, 机器学习, 微软Power BI, 足球赛事分析, Matlab与C#编程 博客园 管理 本站首页 头条推荐 Power BI .NET开源 机器学习 博客美化 ...
- ecshop 国付宝支付接口
function get_code($order, $payment){ $version = '2.2'; $charset = '1'; $language = '1'; $signType = ...
- public,protected,private,static,final的区别(转载)
1.类 (1)在java中有public.protected.private三种显示的修饰符用于控制可见性,package不是显示的修饰符,它是隐含的,即如果在类.变量等前没加显示的可见性修饰符,那它 ...
- IO复用之select实现
前言 在看过前文:初探IO复用后,想必你已对IO复用这个概念有了初步但清晰的认识.接下来,我要在一个具体的并发客户端中实现它( 基于select函数 ),使得一旦服务器中的客户进程被终止的时候,客户端 ...
- 【剑指Offer学习】【面试题62:序列化二叉树】
题目:请实现两个函数,分别用来序列化和反序列化二叉树. 解题思路 通过分析解决前面的面试题6.我们知道能够从前序遍历和中序遍历构造出一棵二叉树.受此启示.我们能够先把一棵二叉树序列化成一个前序遍历序列 ...
- [Phoenix] 五、二级索引
摘要: 目前HBASE只有基于字典序的主键索引,对于非主键过滤条件的查询都会变成扫全表操作,为了解决这个问题Phoenix引入了二级索引功能.然而此二级索引又有别于传统关系型数据库的二级索引,本文将详 ...
- Red Black Tree 红黑树 AVL trees 2-3 trees 2-3-4 trees B-trees Red-black trees Balanced search tree 平衡搜索树
小结: 1.红黑树:典型的用途是实现关联数组 2.旋转 当我们在对红黑树进行插入和删除等操作时,对树做了修改,那么可能会违背红黑树的性质.为了保持红黑树的性质,我们可以通过对树进行旋转,即修改树中某些 ...