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 ...
随机推荐
- 绝不要在构造函数和析构过程中调用virtual函数
下面是一个用来塑模股市交易的类: derived的类的构造函数被调用,但是首先得调用基类Transaction的构造函数,但是在后面还得调用virrual函数,这个时候子类的对象的构造还没有完成,那么 ...
- Jenkins系列之-—08 实现SQL脚本批量执行
公司内部推广DevOps,所有目前在维护阶段和开发阶段项目全部配置上了自动发布.采用Jenkins+SVN+ANT,之后批量执行SQL语句的实现提上日程 一.环境 Linux环境 安装ANT工具,且下 ...
- SMI#、SCI#信号在OS、BIOS、EC中的中断方式(Linux)
EC资料有个很好的CSDN博客推荐去看看:zhao_longwei的专栏 EC固件代码架构:https://blog.csdn.net/zhao_longwei/article/details/510 ...
- tail 监控日志文件
ail命令可以输出文件的尾部内容,默认情况下它显示文件的最后十行.它常用来动态监视文件的尾部内容的增长情况,比如用来监视日志文件的变化.与tail命令对应的是head命令,用来显示文件头部内容. 常用 ...
- Oracle操作笔记
1.查询Oracle版本,数据库的SID select * from v$version; select name from v$database; 2.查询Oracle数据库所支持的功能 SELEC ...
- ssh key 生成
1.设置好git的name和email $ git config --global user.name "姓名" $ git config --global user.email ...
- progressbar请求数据 加载demo1
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...
- Xmpp学习之Asmack取经-asmack入门(一)
1.XMPPConnection:它主要是用来创建一个跟XMPP服务端的Socket连接.它是与Jabber服务端的默认连接并且已经在RFC 3920中精确定义过了.示例如下: XMPPConnect ...
- const与define应用上该怎么取舍
const与define应用上该怎么取舍 #define WYB 100; const float WYB = 100; define是在预编译的时候展开替换的,const是编译运行阶段使用 defi ...
- Appnium安装-Mac平台
Appium的安装-MAC平台 其实Appium的安装方式主要有两种: 1)自己安装配置nodejs的环境,然后通过npm进行appium的安装 2)直接下载官网提供的dmg进行安装,dmg里面已 ...