簡體   English   中英

如何在Web應用程序的Java類中給出絕對的“文件路徑”?

[英]How to give absolute “file-path” in a java class of a web application?

在本地計算機(localhost)上,在Web應用程序的Java類文件中,給出的文件路徑如下。

  JRBeanCollectionDataSource bean = new JRBeanCollectionDataSource(al);
  try {
    print = JasperFillManager.fillReport("D://vivek's//powerSpace//report//bus.jasper",
                                         new HashMap(), bean);
  } catch (JRException e) { 
    e.printStackTrace();
  }
  return al;
}

但是,當此Web應用程序托管在服務器上時,如何給出文件的絕對路徑。

為此使用資源。
例如:
將bus.jasper放入您的Web應用程序的resources文件夾中,然后使用:

InputStream resource = getClass().getClassLoader().getResourceAsStream("bus.jasper");

並從那里使用InputStream。
如果您使用的類的API非常la腳,則需要使用文件路徑

ConstantDataManager.class.getClassLoader().getResource("bus.jasper").getPath();

第二種解決方案在Weblogic上效果不佳。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM