Java PDF创建不需要';不要在指定的位置工作
我想从数据库中的blob字段创建pdf。如果我不指定文件位置,它就可以正常工作:生成pdf并可读。但是如果我指定一个文件位置,我会得到一个Java PDF创建不需要';不要在指定的位置工作,java,mysql,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 retri
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
有人能给出一个建议为什么它不起作用吗?已解决:
我用dir更改了文件的制作,它实现了以下功能:
File file = new File(fileLocation + File.separator + rs.getString("pdfname"));
file.getParentFile().mkdirs();
请发布stacktrace(NPE发生在哪一行?)。并且您确定您的properties对象不是空的-跟踪它?stack:Exception在线程“main”java.lang.NullPointerException中位于be.leerstad.planningsdocumentgenerator.database.mysql.pdf.MysqlPdfDAO.retrievePdf(MysqlPdfDAO.java:153)是,我简单地打印了道具文件的信息,它打印了我需要的信息