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 ...
随机推荐
- idea主要设置大纲图
idea修改主题和字体大小: 对菜单栏进行调整,不过貌似没什么用: 一般设置:
- hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】
Maze Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others) Total Sub ...
- 使用Python与数据库交互
# -*- coding: utf-8 -*- """ Created on Sun Nov 18 19:25:01 2018 @author: wangm " ...
- PythonCookBook笔记——字符串和文本
字符串和文本 使用多个分隔符分割字串 使用正则re.split()方法. >>> line = 'asdf fjdk; afed, fjek,asdf, foo' >>& ...
- 【BZOJ3041】水叮当的舞步 迭代深搜IDA*
[BZOJ3041]水叮当的舞步 Description 水叮当得到了一块五颜六色的格子形地毯作为生日礼物,更加特别的是,地毯上格子的颜色还能随着踩踏而改变.为了讨好她的偶像虹猫,水叮当决定在地毯上跳 ...
- Python3做采集
出于某些目的,需要在网上爬一些数据.考虑到Python有各种各样的库,以前想试试Pycharm这个IDE,就决定用它了.首先翻完<深入Python3>这本书,了解了它的语法之类的.下面就开 ...
- jquery ui 怎么实现tab标签切换效果
1.效果图 2.HTML 代码 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> &l ...
- 谈谈EJB是怎样公布Web Service的
定义 我们常常会听到.xx项目中用到了Web Service.那么.什么是Web Service呢? 首先让我们来了解一下Web Service.Web Service技术.就是能使得执行在不同机器上 ...
- appium()-The event firing
原文地址:https://github.com/appium/java-client/blob/master/docs/The-event_firing.md since 4.1.0 The purp ...
- POJ 1611 The Suspects (并查集+数组记录子孙个数 )
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 24134 Accepted: 11787 De ...