0

我从大型机应用程序中获取了数据。该文件是“sample.txt”文件的名称。当我通过'FTP'使用java代码读取数据以将数据存储到oracle数据库时。它在名称字段中显示一些特殊字符。另外一个信息是我们的产品我们也在使用国际化。我们使用葡萄牙语、西班牙语、英语。我使用独立的 java 程序运行它。

例如给定的输入,

DTALRVAQCRAÜL TORRENTS  +34 93 8640317  +34 93 8640304   RAUL.TORRENS@TRW.COM 20100511FAO
DTALRVAQCRAÜL TORRENTS  +34 93 8640317  +34 93 8640304   RAUL.TORRENS@TRW.COM 20100511FAO

输出为 ,

DTALRVAQCRAðL TORRENTS  +34 93 8640317  +34 93 8640304   RAUL.TORRENS@TRW.COM 20100511FAO
DTALRVAQCRAðL TORRENTS  +34 93 8640317  +34 93 8640304   RAUL.TORRENS@TRW.COM 20100511FAO

什么是我克服这个问题的解决方案。

我的示例 java 文件是,

public void loadData() throws Exception {

          emailMsg.setLength(0);           
          BufferedReader br = null;       
          int r12InsCnt = 0; 
          int r12UpdCnt = 0;
          String strLine = null;          
          int totalrows = 0; int trailerCnt = 0;
          String rundate = "";
          String endDate = "";
          Connection conn = null;        
          CallableStatement cs = null;
          String tmpFilename="";
          List regns = new ArrayList();
          BufferedOutputStream bos = null;
          int buffer = 0;
          InputStream in;
          FileInputStream fstream1=null;
          File tempFile = null;

          String METHOD_NAME = "loadData";            
          try
          {                           
              String DATE_FORMAT = "MMddyyyy";
              java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
              Calendar c1 = Calendar.getInstance();
              String today = sdf.format(c1.getTime()); //mmddyyyy

              CommonUtil cu = new CommonUtil();
              rundate = cu.getCurrentDtTimestamp("yyyy/MM/dd HH:mm:ss.S");            

              //get NAS server details from DynoProp table
              PropertyManager propmanager = PropertyManager.getInstance();            
              PropertyGroup vrpgProGroup = propmanager.getGroup("ServerCredentials");       
              String user = vrpgProGroup.getString("id");             
              String password = vrpgProGroup.getString("password"); 
              String serverName = vrpgProGroup.getString("server");

              FtpClientAttributes ftpAttribs = new FtpClientAttributes.Builder(serverName,user,password).build();
              FtpProxyClient ftp = new FtpProxyClient(ftpAttribs);                  
              //ftp.setBinaryMode();
              ftp.setAsciiMode();
              ftp.changeDirectory("Vrpg");

              if(ftp.isFileExists("vrpg.contacts.txt"))
              {
                     tmpFilename = "vrpg_contacts_Pr_" + today + ".txt";
                     ftp.renameFile("vrpg.contacts.txt", tmpFilename);
                     in = ftp.getFile(tmpFilename);

                     //String tempDir = "/proj/mingle/was/install/mingle_mingleEAR.ear/mingleWeb.war/vrpg/";
                     String tempDir = System.getProperty("java.io.tmpdir") + "/";                    

                     if("DESKTOP".equalsIgnoreCase(System.getProperty("DEPLOYED_ENVIRONMENT"))) {
                         tempDir = "C:/SDPRNG/vrpg/"; 
                     }

                     bos = new BufferedOutputStream(new FileOutputStream(tempDir + tmpFilename));                                        
                      while ((buffer = in.read()) != -1) {
                          bos.write(buffer);
                      }
                      bos.flush();
                      bos.close();                    
                      ftp.cleanup();

                      tempFile = new File(tempDir + tmpFilename);
                      fstream1 = new FileInputStream(tempFile);
                      br = new BufferedReader(new InputStreamReader(fstream1,"CP1252"));

                      conn = DBConnection.getmingleConnection();
                      PreparedStatement pstmt = null;

                        try{
                            if(conn!=null){
                            pstmt = conn.prepareStatement(mingleQuery.UPDATER12DELETEFLAG);
                            pstmt.execute();
                            }
                        }catch(SQLException e){
                            throw new mingleBusinessException(
                                    new ruralExceptionAttributes.Builder(
                                    CLASS_NAME, METHOD_NAME).build(),
                                    "Getting exception while updating the delete flag in loadData ", e);
                        }

                      while ((strLine = br.readLine()) != null)                       
                      { 
                          try
                          {
                               //Header:"AA", Trailer:"ZZ", Detail:"DT" record.
                               if(strLine.substring(0,2).equalsIgnoreCase("ZZ"))
                               { trailerCnt = Integer.parseInt(strLine.substring(36,43)); }

                               if(strLine.substring(0,2).equalsIgnoreCase("DT"))
                               {                  
                                  totalrows++;
                                  System.out.println("The Values is :"+strLine.length());
                                  //System.out.println("calling SP for:" + strLine.substring(2,7));
                                  //insert to R12
                                   try
                                   {                   
                                     cs = null;
                                     cs = conn.prepareCall("{call createDetailContactRawdata(?,?,?,?,?,?,?,?,?,?,?,?)}");                      
                                     cs.setString(1, strLine.substring(2,7).trim());  //sitecode
                                     cs.setString(2, "vrpg");
                                     cs.setString(3, strLine.substring(7,9).trim()); //title                     
                                     cs.setString(4, strLine.substring(160).trim()); //org
                                     cs.setString(5, strLine.substring(9,41).trim()); //name
                                     cs.setString(6, strLine.substring(41,63).trim()); //phone
                                     cs.setString(7, strLine.substring(63,85).trim()); //cell
                                     cs.setString(8, strLine.substring(85,107).trim()); //fax
                                     cs.setString(9, strLine.substring(107,152).trim()); //email
                                     cs.setString(10, strLine.substring(152,160));      //last update date                  
                                     cs.registerOutParameter(11, java.sql.Types.VARCHAR);
                                     cs.registerOutParameter(12, java.sql.Types.CHAR);
                                     cs.execute();                          
                                     if(!"S".equals(cs.getString(11))){
                                        emailMsg.append(cs.getString(11));                                          
                                     } 
                                     else 
                                     { 
                                        if(cs.getString(12).equalsIgnoreCase("I")) r12InsCnt++;
                                        if(cs.getString(12).equalsIgnoreCase("U")) r12UpdCnt++;
                                     }
                                     cs.close();                                                    
                                   } catch(Exception e) {
                                     System.out.println("sp error:" + e.getMessage());  
                                     emailMsg.append("Error occured executing SP:" + strLine.substring(2,7)+strLine.substring(7,9).trim()+ strLine.substring(160).trim()+ e.getMessage() + "\n");
                                   }
                               }//end if DT

                          }catch(Exception e){
                             System.out.println("error inside while" + e.getMessage()); 
                             emailMsg.append("Error reading vrpg contacts file:" +  strLine.substring(0,25)+ e.getMessage()+ "\n");
                             log.logp(Level.INFO,CLASS_NAME,"loadData", "error reading vrpg contacts file" + e.getMessage().toString()); 
                          }

                      } //end while

                      try{
                            if(conn!=null){
                            pstmt = conn.prepareStatement(mingleQuery.DELETER12_DELETEFLAG_Y);
                            pstmt.execute();
                            }
                        }catch(SQLException e){
                            throw new mingleBusinessException(
                                    new ruralExceptionAttributes.Builder(
                                    CLASS_NAME, METHOD_NAME).build(),
                                    "Getting exception while deleting the records with delete flag 'Y' in loadData ", e);
                        }

                      br.close();
                      conn.close();
                      if(pstmt != null) pstmt.close();

                      //System.out.println("Total Rows ** :" + totalrows); 
                      //System.out.println("Trailer Count** :" + trailerCnt); 

                      if(totalrows < trailerCnt) { emailMsg.append("Total rows processed is less than trailer record count:" + totalrows + "/" + trailerCnt + "\n"); }

                     /*System.out.println("r12InsCnt:" + r12InsCnt);                 
                      System.out.println("r12UpdCnt:" + r12UpdCnt);*/

                     endDate = cu.getCurrentDtTimestamp("MM/dd/yyyy HH:mm:ss");
                     //create log for batch process with number of rows retrieved from vrpg and rows inserted into mingle
                     BatchLogBO batchLogBO = new BatchLogBO();
                     batchLogBO.setBusnType(busnType);          
                     batchLogBO.setRatingRegion(region);            
                     batchLogBO.setStartDate(rundate);
                     batchLogBO.setEndDate(endDate);
                     batchLogBO.setRowsReceived(totalrows);
                     batchLogBO.setRowsInserted(r12InsCnt);
                     batchLogBO.setRowsUpdated(r12UpdCnt);
                     batchLogBO.setRowsDeleted(0);
                     batchLogBO.setBatchName("vrpgCONATCTS");
                     batchLogBO.setBatchType("DAILY");
                     batchLogBO.setvrpgCode("NONE");
                     batchLogBO.setLastUpdtPgmCode("BATCH");
                     try
                     { 
                          mingle_BINDING_AS.beginTransaction();
                          batchLogBO = mingle_BINDING_AS.bindCreateOrUpdate(batchLogBO);
                          mingle_BINDING_AS.commitTransaction();
                     } catch(Exception e) {
                          emailMsg.append("Error inserting batchLog:" + e.getMessage() + "\n");
                     }                                      
                     //System.out.println("about to run load instance for all regions");
                     try
                     {
                         //call loadInstance method to load instance data for each region                    
                         regns = getRegions();
                         for(int k=0; k<regns.size(); k++) {                         
                           loadInstance(cu.getCurrentDtTimestamp("MM/dd/yyyy"),regns.get(k).toString());
                         }
                     }catch(Exception e) {
                        emailMsg.append("Error getting regions to run loadInstance:" + e.getMessage() + "\n"); 
                     }

                 } else {                  
                   Mailbox.sendMail("rpgsupp@rural.com", "rpgsupp@rural.com", System.getProperty("DEPLOYED_ENVIRONMENT"), "vrpg Contacts file not found on NAS");
                   return;
                 }

                 if ( (emailMsg.toString().length() > 1) && (!emailMsg.toString().equalsIgnoreCase(null)) && (!emailMsg.toString().equalsIgnoreCase("null")) ) {     
                     Mailbox.sendMail("rpgsupp@rural.com", "rpgsupp@rural.com", System.getProperty("DEPLOYED_ENVIRONMENT")+ ":Load vrpg Contacts Details",  emailMsg.toString());
                 }

                 tempFile.delete(); //delete tmp file from app server

                 ftp = new FtpProxyClient(ftpAttribs);
                 emailMsg.append(cu.deletefile(ftp,today,"vrpg_Contacts_")); //delete old files from NAS
                 ftp.cleanup();

            } catch(Exception e) {
                System.out.println("inside final exception:" + e.getMessage());
                if(br != null) br.close();              
                if(conn !=null) conn.close();
                emailMsg.append("Error:" + e.getMessage()+ "\n");
                Mailbox.sendMail("rpgsupp@rural.com", "rpgsupp@rural.com", System.getProperty("DEPLOYED_ENVIRONMENT")+ ":Load vrpg Contacts Details",  emailMsg.toString());
                log.logp(Level.INFO,CLASS_NAME,"loadData", "error getting vrpg contacts file from NAS:" + e.getMessage().toString());
            }

    }
4

2 回答 2

4

这是您的问题,很可能是:

br = new BufferedReader(new InputStreamReader(fstream1,"CP1252"));

通过假设它们使用 CP1252 编码,您正在将文件的字节转换为字符。

如果文件的实际编码不是CP1252,则字节将映射为错误的字符。(几乎每种编码都对“ASCII 字符”使用相同的值,因此您只会看到“特殊”字符的错误。)是什么让您决定使用 CP1252 进行解码,您确定这是正确的吗?

要解决您的问题,请找出这些文件使用的实际编码,并将其传递给 InputStreamReader 构造函数。

于 2012-06-28T12:04:00.177 回答
0

与大型机通信时需要小心。这取决于您通过 FTP 传输文件的方式,因为它可能会在传输中自动转换为 ASCII。

通常,二进制传输将为您提供本机 EBCDIC,您需要将其转换为 ASCII。如果您的文件包含二进制数字(例如 COMP3),您可能不想转换流中的数据。

US EBCDIC 代码页通常为 Cp1047,这是将字符串转换为 ASCII 的代码示例。您可以将任何代码页文本(美国的“Cp1047”)传递给您的文件的例程:

    // Convert Binary EBCDIC Data to String
public static String toASCII(byte[] arr, int offset, int len, String codepage){
    try {
        return new String(arr, offset, len, codepage);
    } catch (UnsupportedEncodingException e) {
        System.out.println("Error Occurred translating EBCDIC data to String");
        e.printStackTrace();
        return null;
    }
}
于 2012-06-28T14:18:20.350 回答