2016-06-28 6 views
0

robotframework의 경우 바이트 배열을 String으로 변환 할 수있는 코드를 작성해야합니다. 하지만 그 전에 바이트 배열의 크기를 알아야합니까? 바이트 배열을 robotframework의 String으로 변환합니다.

나는이 하나 시도 :

@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[] b = ...; //However you got that byte[] 
String charsetName = "UTF-8"; //Or whatever the charset was using for encoding 
String result = new String(b,charsetName); 

바이트 세트 []가 인코딩 된 charset을 알아야합니다. 일반적인 값은 "UTF-8"또는 "ISO-8859-1"입니다.

0

당신은 내가 조금 혼란 스러워요 로봇 파일

${temp} = Convert To Bytes 0102030405 hex 
관련 문제