java请求C# asmx接口

本文介绍了一个使用Spring框架的控制器,通过WebService接口上传PDF文件,并将其转换为Base64编码进行传输的方法。该示例包括了如何生成唯一标识符、处理文件流以及设置WebService调用参数的详细步骤。
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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值