大家好,又见面了,我是你们的朋友全栈君。
package com.example.demo.controller;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.BASE64Encoder;
import javax.xml.namespace.QName;
import java.io.*;
import java.rmi.ConnectException;
import java.util.Random;
import java.util.UUID;
/**
* Created with IDEA
* author:QinWei
* Date:2019/12/2 0002
* Time:16:57
*/
@Controller
public class indexController {
@RequestMapping("/index")
@ResponseBody
public Object index(){
return WebService();
}
public static String WebService(){
File file = new File("C:\\pdf.pdf");
String str = PDFToBase64(file);
// webService链接地址 正式环境请更换至配置文件中以防修改方便
String url = "http://IP/XXX.asmx";
String uuid = genUniqueKey();
// server域名地址,为了统一规范,一般都是这个域名 固定地址不需要配置
String soapaction = "http://tempuri.org/";
// 方法名
String methodName = "DocumentPush";
// 用户提供测试的两个参数
String action = "Create";
String ref = null;
StringBuffer sb = new StringBuffer();
//XML拼接
sb.append("<REQUEST>");
sb.append("<RESPONSEITEM>");
sb.append("<PATIENTID>T001688785</PATIENTID>");
sb.append("</RESPONSEITEM>");
sb.append("</REQUEST>");
System.out.println("=========打印============"+sb);
Service service = new Service();
try{
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(url);
// 设置要调用哪个方法
call.setOperationName(new QName(soapaction,methodName));
// 设置要传递的参数名
call.addParameter(new QName(soapaction,"action"),org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName(soapaction,"message"),org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
// 提供标准类型
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapaction + methodName);
// 调用方法并传递参数
ref = (String)call.invoke(new Object[]{action,sb.toString()});
System.out.println("=========打印============"+ref);
return ref;
}catch (ConnectException ex){
ex.printStackTrace();
System.err.println("=========连接超时=========");
} catch (Exception e){
e.printStackTrace();
}
ref = "链接失败";
return ref;
}
public static void main(String []ages){
//WebService();
//System.out.println(str);
}
/*
生成唯一主键
格式:时间+随机数
*/
public static synchronized String genUniqueKey() {
Random random = new Random();
Integer number = random.nextInt(900000) + 100000;
return System.currentTimeMillis() + String.valueOf(number);
}
/**
* Description: 将pdf文件转换为Base64编码
* @param file
* @Author qinwei
* Create Date: 2015年8月3日 下午9:52:30
*/
public static String PDFToBase64(File file) {
BASE64Encoder encoder = new BASE64Encoder();
FileInputStream fin =null;
BufferedInputStream bin =null;
ByteArrayOutputStream baos = null;
BufferedOutputStream bout =null;
try {
fin = new FileInputStream(file);
bin = new BufferedInputStream(fin);
baos = new ByteArrayOutputStream();
bout = new BufferedOutputStream(baos);
byte[] buffer = new byte[1024];
int len = bin.read(buffer);
while(len != -1){
bout.write(buffer, 0, len);
len = bin.read(buffer);
}
//刷新此输出流并强制写出所有缓冲的输出字节
bout.flush();
byte[] bytes = baos.toByteArray();
return encoder.encodeBuffer(bytes).trim();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fin.close();
bin.close();
bout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
以下为需要的maven依赖
<!-- 以下均为webservice调用的maven依赖 -->
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.5</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/145055.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...