2016-06-28 42 views
0

對於robotframework,我需要編寫一個可以將字節數組轉換爲字符串的代碼。但在此之前,我需要知道字節數組的大小嗎?將字節數組轉換爲機器人框架中的字符串

我這個嘗試之一:

@SuppressWarnings("deprecation") 
    public static byte[] retrievePolicy(String aPolicyNumber) { 

     String METHOD_NAME="retrievePolicy"; 
     byte[] myBinData = null; 
     Connection myConnection = null; 
     String mySelectSql = "SELECT PSD_BIN_DATA FROM XAT_POLICY_STUB_DATA WHERE PSD_POLICY_NBR = ?"; 
     String myWorkingQuerySelect; 

     //Statement myStmt = null; 
     PreparedStatement myPreparedStatement = null; 
     String myJndiName; 

     try { 
      DataSource myDataSource; 
      myJndiName = AllcorpProperties.getProperty(ALLIANCE_STUB_DATASOURCE_JNDI_NAME); 
      myWorkingQuerySelect= mySelectSql; 

      myDataSource = ServiceLocator.obtainDataSource(myJndiName); 
      if(myDataSource!=null) { 
       myConnection = myDataSource.getConnection(); 
       if(myConnection != null){ 
        //myStmt = myConnection.createStatement(); 
        myPreparedStatement = myConnection.prepareStatement(myWorkingQuerySelect); 
        myPreparedStatement.setString(1, aPolicyNumber); 
        ResultSet resultSet = myPreparedStatement.executeQuery(); 

        if (resultSet != null && resultSet.next()) { 
         Blob myBlob = resultSet.getBlob(1); 
         if (myBlob != null) { 
          myBinData = myBlob.getBytes((long) 1, (int) myBlob.length()); 
         } 
        } 
       }else{ 
        PolicyServiceException myPolicyServiceException = new PolicyServiceException(new AllcorpException(), 
          EXCEPTION_OBTAINING_CONNECTION, 
          PolicyDAO.CDB_COMMUNICATION_ERROR, METHOD_NAME,BUSINESS_FUNCTION_NAME); 
        AllcorpLogger.error(CLASS_NAME,myPolicyServiceException); 
        throw myPolicyServiceException; 
       } 

      }else{ 
       PolicyServiceException myPolicyServiceException = new PolicyServiceException(new AllcorpException(), 
         EXCEPTION_STATEMENT_GET_DATASOURCE, 
         PolicyDAO.CDB_COMMUNICATION_ERROR, METHOD_NAME,BUSINESS_FUNCTION_NAME); 
       AllcorpLogger.error(CLASS_NAME,myPolicyServiceException); 
       throw myPolicyServiceException; 
         } 

     } catch (SQLException mySQLException) { 

      AllcorpLogger.error(CLASS_NAME,new PolicyServiceException(mySQLException, 
      "Error occurred while retreiving policy from stub DB for policy number "+aPolicyNumber,"SDBURTP",METHOD_NAME,BUSINESS_FUNCTION_NAME)); 
     } catch(AllcorpInfrastructureException myAllcorpInfrastructureException) 
     { 
      PolicyServiceException myAllcorpException = new PolicyServiceException(
        myAllcorpInfrastructureException, "Exception while obtaining the data source ", 
        "STUBDBRP", METHOD_NAME, BUSINESS_FUNCTION_NAME); 
      AllcorpLogger.error(CLASS_NAME, myAllcorpException); 
     } 
     catch (Exception myException) { 

      AllcorpLogger.error(CLASS_NAME,new PolicyServiceException(myException, 
        "Error occurred while retreving policy from StubDB for policy number " +aPolicyNumber,"STUBDRP2",METHOD_NAME,BUSINESS_FUNCTION_NAME)); 
     }finally { 
      try { 
       if (myPreparedStatement != null) { 
        myPreparedStatement.close(); 
       } 
       if(myConnection != null){ 
       myConnection.close(); 
       } 

      } catch (SQLException myException) { 
       AllcorpLogger.error(CLASS_NAME,new PolicyServiceException(myException, 
         "Error closing prepared statement object","SDBURTP1",METHOD_NAME,BUSINESS_FUNCTION_NAME)); 
      } 
     } 
     return myBinData; 
    } 
+0

,我需要的是一個(BLOB)政策 –

+0

我有點困惑。你發佈了一大段代碼,但我不知道你的問題在哪裏?請說明爲什麼你不能使用例如'new String(byte [] bytes)'或'DatatypeConverter.printBase64Binary(byte [] val)'並且 - 如果可能的話減少代碼示例。 – Rhayene

回答

0

如果你有一個byte []您可以使用 新的字符串(字節[],字符集),例如,將其轉換爲字符串

byte[] b = ...; //However you got that byte[] 
String charsetName = "UTF-8"; //Or whatever the charset was using for encoding 
String result = new String(b,charsetName); 

你必須知道,字節[]在哪個字符集中被編碼。典型值爲 「UTF-8」或「ISO-8859-1」。

0

爲此,您可以在您的機器人文件

${temp} = Convert To Bytes 0102030405 hex 
相關問題