2013-03-27 34 views
2

我有一个读取和写入数据的servlet。这里是我的代码片段org.opendatafoundation.data.spss.SPSSFileException:读取数据时出错:字符串变量的意外压缩代码

protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    response.setContentType("text/html;charset=UTF-8"); 
    PrintWriter out = response.getWriter(); 
    SPSSFile file = null; 
    try { 
     File f = new File(getServerDiretory() + "dabadeba_2011.01.03.sav"); 

     if (!f.exists()) { 
      System.out.println("not found"); 
      return; 
     } 
     file = new SPSSFile(f); 

     file.loadMetadata(); 
     file.loadData(); 

     if (file == null) { 
      System.err.println("vai"); 
      return; 
     } 

     Document doc = file.getDDI2(); 

     //set up a transformer 
     TransformerFactory transfac = TransformerFactory.newInstance(); 
     Transformer trans = transfac.newTransformer(); 
     trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); 
     trans.setOutputProperty(OutputKeys.INDENT, "yes"); 
     trans.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "yes"); 

     StringWriter sw = new StringWriter(); 
     StreamResult result = new StreamResult(sw); 
     DOMSource source = new DOMSource(doc); 
     trans.transform(source, result); 
     String xmlString = sw.toString(); 

     writeToFile(xmlString); 

     out.println(xmlString); 

    } catch (TransformerException ex) { 
     Logger.getLogger(SPSSReaderServlet.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (FileNotFoundException ex) { 
     Logger.getLogger(SPSSReaderServlet.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (SPSSFileException ex) { 
     Logger.getLogger(SPSSReaderServlet.class.getName()).log(Level.SEVERE, null, ex); 
    } finally { 
     out.close(); 
     if (file != null) { 
      file.close(); 
      System.out.println("done, file closed"); 
     } 
    } 
} 

一切似乎都在工作,直到我刷新JSP并再次调用此servlet。以下是刷新页面后出现的错误:

SEVERE: org.opendatafoundation.data.spss.SPSSFileException: Error reading data: unexpected compression code for string variable 
    at org.opendatafoundation.data.spss.SPSSDataRecord.read(SPSSDataRecord.java:161) 
    at org.opendatafoundation.data.spss.SPSSDataRecord.read(SPSSDataRecord.java:54) 
    at org.opendatafoundation.data.spss.SPSSFile.loadData(SPSSFile.java:1277) 
    at ge.geostat.metadata.web.servlet.SPSSReaderServlet.processRequest(SPSSReaderServl‌​et.java:63) 
    at ge.geostat.metadata.web.servlet.SPSSReaderServlet.doGet(SPSSReaderServlet.java:1‌​40) 

当我重新部署应用程序并运行它时,它工作正常。我想这是一个内存问题,任何帮助将不胜感激

+0

请检查catalina日志文件(如果您使用tomcat)并让我们知道发生异常的位置。 – boomz 2013-03-27 23:52:14

+0

我使用Glasfish,这里是空穴例外: 重度:org.opendatafoundation.data.spss.SPSSFileException:错误读取数据:对字符串变量意外压缩码 \t在org.opendatafoundation.data.spss.SPSSDataRecord.read( SPSSDataRecord.java:161) \t在org.opendatafoundation.data.spss.SPSSDataRecord.read(SPSSDataRecord.java:54) \t在org.opendatafoundation.data.spss.SPSSFile.loadData(SPSSFile.java:1277) \t at ge.geostat.metadata.web.servlet.SPSSReaderServlet.processRequest(SPSSReaderServlet.java:63) \t at ge.geostat.metadata.web.servlet.SPSSReaderServlet.doGet(SPSSReaderServlet.java:140) – 2013-03-27 23:54:32

+0

将数据写入文件后关闭文件吗? – boomz 2013-03-27 23:59:17

回答

0

谢谢你们的帮助。我已经找到了问题,它可能有助于某人使用org.opendatafoundation SPSS阅读器类

因此,问题出在loadData()方法,它使用静态变量,必须在第二个调用:SPSSDataRecord.clusterIndex = 8;

/** 
* Load the data section of the file into the variables in memory. This may be expensive on memory, use with care on large datasets 
* 
* @throws SPSSFileException 
* @throws IOException 
*/ 
public void loadData() throws IOException, SPSSFileException { 
    if (dataStartPosition < 1) { 
     // this has not been initialized, we don't actually know where the data starts 
     throw new SPSSFileException("Error: data location pointer not initialized."); 

    } 
    SPSSDataRecord data = new SPSSDataRecord(); 

    SPSSDataRecord.clusterIndex = 8; //---This is the fix!!! 
    seek(dataStartPosition); 
    for (int i = 0; i < infoRecord.numberOfCases; i++) { 
     // log("\nRECORD "+(i+1)+" offset "+this.getFilePointer()); 
     data.read(this); 
    } 
    isDataLoaded = true; 
} 
1

因为刷新页面时会发生异常,因为您忘记关闭文件,所以会发生异常。我相信问题就在于此。因为你第一次建立你的项目是有效的,在那之后它不起作用。您应该在finally区块中调用close文件的方法。

+0

你能详细说一下吗?这听起来更像是一个评论。 – bfavaretto 2013-03-28 00:53:57

+0

我们在上面的评论中有一些协作。所以我解释了一下!该帖子已被编辑! – boomz 2013-03-28 01:01:11

+0

从对这个问题的评论中,*“仍然没有任何运气”的哪个部分*你不了解?在评论中发布的代码也只显示'finally'中的关闭调用。你错过了吗? – BalusC 2013-03-28 01:05:26