码迷,mamicode.com
首页 > Web开发 > 详细

WebService完成文件上传下载

时间:2018-02-22 10:51:32      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:三种方式   lenovo   存储   enc   lin   tor   引用   space   字符串   

由于开发需要使用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 实现文件的上传下载(非自动生成)

其他参考:

   使用Axis2方式发布webService的三种方式

   axis2 jar包详解及缺少jar包错误分析

WebService完成文件上传下载

标签:三种方式   lenovo   存储   enc   lin   tor   引用   space   字符串   

原文地址:https://www.cnblogs.com/Dream2hc/p/java0254307.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!