[英]PDF Creation doesn't work in specified location
我想從數據庫中的blob字段創建pdf。 如果我不指定文件位置,它就可以正常工作:pdf生成且可讀。 但是,如果指定文件位置, nullpointerexception
得到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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.