【问题标题】:To convert a byte array to String in robotframework在机器人框架中将字节数组转换为字符串
【发布时间】:2016-10-30 22:07:22
【问题描述】:

对于机器人框架,我需要编写一个可以将字节数组转换为字符串的代码。但在此之前,我需要知道字节数组的大小吗?

我试过这个:

@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 retrieving 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;
    }

【问题讨论】:

  • 我需要的策略是(BLOB)
  • 我有点困惑。您发布了一大段代码,但我不知道您的问题出在哪里?请说明您为什么不能使用例如 new String(byte[] bytes)DatatypeConverter.printBase64Binary(byte[] val) 和 - 如果可能的话,减少代码示例。

标签: java robotframework


【解决方案1】:

如果你有一个 byte[],你可以将它转换为 String 使用 new String(byte[],charset),例如

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);

您必须知道 byte[] 是在哪个字符集中编码的。典型值为 “UTF-8”或“ISO-8859-1”。

【讨论】:

    【解决方案2】:

    您可以在您的机器人文件中执行此操作

    ${temp} =    Convert To Bytes    0102030405    hex
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      • 2018-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多