0

我想从我的数据库中的 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);
    }
}

在propertyfile中写着:FileLocation=reports

谁能给一个建议,为什么它不起作用?

4

1 回答 1

1

解决了 :

我用 dir 更改了文件的制作,它成功了:

File file = new File(fileLocation + File.separator + rs.getString("pdfname"));
                    file.getParentFile().mkdirs(); 
于 2012-05-19T11:16:45.360 回答