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 實現文件的上傳下載(非自動生成)

其他參考:

   使用Axis2方式發布webService的三種方式

   axis2 jar包詳解及缺少jar包錯誤分析


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM