PDF创建在指定位置不起作用

时间:2012-05-19 10:33:56

标签: java mysql

我想从我的数据库中的blob字段创建一个pdf。如果我没有指定文件位置,它的工作正常:pdf是生成和可读的。但是,如果我指定文件位置,我会得到nullpointerexception

文件位置位于属性文件中。

/**
 * Deze methode zoekt een pdf in de database op pdfnaam
 * 
 * @param name, de naam van de pdf
 * @return maakt de pdf aan
 */
public void retrievePdf(int iddocument) {
    Properties prop = new Properties();
    String fileLocation = new String("");
    FileOutputStream fos = null;

    try {
        // load a properties file
        prop.load(new FileInputStream("props/config.properties"));

        // get the property value and use it
        fileLocation = prop.getProperty("FileLocation");

        // verwijderen
        System.out.println(fileLocation);

    } catch (IOException ex) {
        ex.printStackTrace();
    }

    try {
        c = MySqlDAOFactory.getInstance().getConnection();

        String sql = "select * from pdf where iddocument=?";
        prest = c.prepareStatement( sql );
        prest.setInt(1, iddocument);
        rs = prest.executeQuery();

        while (rs.next()) { 
            // create file with the filename from 
            // the db in the dir fileLocation
            File file = new File(fileLocation, rs.getString( "pdfname" ) );
            //get the blob from the db
            Blob blob = rs.getBlob( "pdffile" );
            InputStream is = blob.getBinaryStream();   
            try {
                fos = new FileOutputStream(file);
            } catch (FileNotFoundException e) {
                // ...
            }  
            byte [] buffer = new byte[(int)blob.length()];
            int length = -1;
            try {
                while( ( length = is.read( buffer ) ) != -1 ) {
                    try {
                        fos.write(buffer, 0, length);
                    } catch (IOException e) {
                        // ...
                    }
                    try {
                        fos.flush();
                        fos.close();
                    } catch (IOException e) {
                        // ...
                    }
                }
            } catch (IOException e) {
                // ...
            }

            return;
        }
    } catch (SQLException e) {
        // ...
    } finally {
        MySqlConnectionFactory.getInstance().closeResultSet(rs);
        MySqlConnectionFactory.getInstance().closeStatement(prest);
        MySqlConnectionFactory.getInstance().closeConnection(c);
    }
}

在属性文件中写道:FileLocation=reports

任何人都可以提出为什么它不起作用的建议吗?

1 个答案:

答案 0 :(得分:1)

解决了:

我用dir改变了文件的制作,它就是诀窍:

File file = new File(fileLocation + File.separator + rs.getString("pdfname"));
                    file.getParentFile().mkdirs();