1. 程式人生 > >AXIS2實現檔案上轉下載測試通過

AXIS2實現檔案上轉下載測試通過

 

應用AXIS2框架向服務端上傳檔案:

服務端程式碼:

public void uploadImageWithByte(byte[] imageByte, int length){
  FileOutputStream fos = null;
        try
        {
            //  將上傳的影象儲存在D盤的test1.jpg檔案中
            fos = new FileOutputStream("c:/test/test1.DAT");
            //  開始寫入影象檔案的位元組
            fos.write(imageByte, 0, length);
            fos.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (fos != null)
            {
                try
                {
                    fos.close();
                }
                catch (Exception e)
                {
                }
            }
        }
       
    }

客戶端程式碼:

RPCServiceClient serviceClient;
  try {
   serviceClient = new RPCServiceClient();
    Options options = serviceClient.getOptions();
          EndpointReference targetEPR = new EndpointReference(ClientCommonParameter.NotFoundsUrl);
          options.setTo(targetEPR);
          // 下面的程式碼呼叫uploadImageWithByte方法上傳影象檔案
          /////////////////////////////////////////
          // 開啟影象檔案,確定影象檔案的大小
          java.io.File file = new java.io.File("c:/Upload/test1.DAT");
          java.io.FileInputStream fis = null;
   
    fis = new java.io.FileInputStream("c:/Upload/test1.DAT");
    
          // 建立儲存要上傳的影象檔案內容的位元組陣列
          byte[] buffer = new byte[(int) file.length()];
          // 將影象檔案的內容讀取buffer陣列中
          int n = 0;
   
    n = fis.read(buffer);
     //n為讀入的位元組數
          System.out.println("檔案長度:" + file.length());
          Object[] opAddEntryArgs = new Object[]{ buffer, n };
          Class[] classes = new Class[]{ };
          QName opAddEntry = new QName(ClientCommonParameter.Qurl,"uploadImageWithByte");
          fis.close();    
          serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes);
          // 開始上傳影象檔案,並輸出uploadImageWithByte方法的返回傳
         // System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]);
  } catch (AxisFault e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }


======================================================================
檔案下載客戶端:

 RPCServiceClient serviceClient;
   try {
    serviceClient = new RPCServiceClient();
   
            Options options = serviceClient.getOptions();
            EndpointReference targetEPR = new EndpointReference(ClientCommonParameter.NotFoundsUrl);
            options.setTo(targetEPR);
   
            Object[] opAddEntryArgs = new Object[] {};
            Class[] classes = new Class[] {byte[].class};
            QName opAddEntry = new QName(ClientCommonParameter.Qurl, "down");
            byte[] strArray = (byte[])serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0];
          
            FileOutputStream fos = null;
           
                //  將下載的影象儲存在D盤的test1.jpg檔案中
                fos = new FileOutputStream("c:/test/bb.DAT");
                //  開始寫入影象檔案的位元組
                fos.write(strArray);
                fos.close();
           
            System.out.println("OK");
   } catch (AxisFault e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
        }


服務端:
 public byte[] down() throws Exception
        {
           // 下面的程式碼呼叫uploadImageWithByte方法上傳影象檔案
           // 開啟影象檔案,確定影象檔案的大小
            java.io.File file = new java.io.File("c:/test/test1.DAT");
            java.io.FileInputStream fis = new java.io.FileInputStream
                                          ("c:/test/test1.DAT");
            // 建立儲存要上傳的影象檔案內容的位元組陣列
            byte[] buffer = new byte[(int) file.length()];
            // 將影象檔案的內容讀取buffer陣列中
            int n = fis.read(buffer);  //n為讀入的位元組數
            System.out.println("檔案長度:" + file.length());
            fis.close();
            return buffer;
        }