无法读取文件Spring

时间:2016-04-28 00:30:40

标签: java spring

我正在为此application编写自定义控制器。你可以在/ ssf / samples / customcontroller下找到一个样本。对于我的customcontroller,我使用了Spring-Framework,它也被kablink项目使用。现在我尝试通过getSimpleJdbcTemplate().queryForObject(query, String.class, new MapSqlParameterSource());执行sql脚本(my.sql)。 sql文件只包含一行select label from question where questionid=3;

但是我通过读取my.sql文件遇到了困难。在Controller中,我调用方法graph.sunburstJson()并将返回值推送到视图:

ApplicationContext context = new ClassPathXmlApplicationContext("/resources/spring/config/GraphBean.xml");
        GraphDao graph = (GraphDao) context.getBean("graphDao");
response.setRenderParameter("json", graph.sunburstJson());

要查看方法sunburstJson是否读取文件,我尝试返回my.sql的内容:

public class GraphDaoImpl extends SimpleJdbcDaoSupport implements GraphDao{

    @Override
    public String sunburstJson() {
        String query="select label from question where questionid=5";

        try {
            InputStream is = getClass().getResourceAsStream("/resources/spring/sql/my.sql");
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            LineNumberReader fileReader = new LineNumberReader(reader);
            query = JdbcTestUtils.readScript(fileReader);
        } catch (IOException e) {
            StringWriter errors = new StringWriter();
            e.printStackTrace(new PrintWriter(errors));
            query=errors.toString()+"help";
        }
        return query+"test";//(String)getSimpleJdbcTemplate().queryForObject(query, String.class, new MapSqlParameterSource());

    }
}

但它没有返回任何错误,没有输出,我无法解释发生了什么。这从第JdbcTestUtils.readScript行开始。与

相同的行为
EncodedResource resource = new EncodedResource(context.getResource("/resources/spring/sql/my.sql"), "UTF-8");

如果我尝试

BufferedReader reader = new BufferedReader(new FileReader(new File("resources/spring/sql/my.sql")));
 //or
BufferedReader reader = new BufferedReader(new FileReader("/resources/spring/sql/my.sql"));

我找到了一个未找到异常堆栈跟踪的文件。

1 个答案:

答案 0 :(得分:0)

我为项目添加了library的新版本,现在它正常运行。