2

我有一个读取和写入数据的 servlet。这是我的代码片段

    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)

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

4

2 回答 2

1

因为刷新页面时会出现异常,是因为忘记关闭文件而发生的。我确信问题是这样的。因为第一次构建项目时它可以工作,之后它就不起作用了。您应该在块中调用close文件的方法。finally

于 2013-03-28T00:29:25.763 回答
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;
}
于 2013-03-29T11:30:22.393 回答