How to give path to the PDF file while creation in struts 2?
I am trying to create a PDF file using struts 2.Action class location is as follows.
/home/Jagan/MATCH/Jagan/src/ActionClasses/PDFFile.java
Here workspace starts from
/MATCH
In PDFFile.java.I am writing as given below and it is working fine.
pdfwriter=PdfWriter.getInstance(document,new FileOutputStream("/home/Jagan/xyz.pdf"));
But i have to create this under the folder
/ho开发者_JAVA百科me/Jagan/MATCH/Jagan/PDFs
I should not use /home/Jagan/
as it will become hardcode if i have to run this application in other system.
I tried
pdfwriter=PdfWriter.getInstance(document,new FileOutputStream("../../../PDFs/xyz.pdf"));
But it is not creating file.Even if it works it is not feasible solution (because "../../../" does not work in windows ).
Please suggest me a good way to specify path for creating file.
Adding to the question.
I have to provide download option for downloading this created file in JSP page.Which struts tag should i use. Please provide me syntax for that
downloading files has 2 aproaches: 1. you can return it with outstream result like is explained here 2. as you are trying to save the file first at filesystem then access it from another url. Answer to your question is you should get servletContext.getRealPath("/WEB-INF"), and after that everything is relative to WEB-INF.
I should not use /home/Jagan/ as it will become hardcode if i have to run this application in other system.
Correct, Use following instead
System.getProperty("user.home");///home/Jagan/, it will return you path to your home dir
精彩评论