0

I have to get input any kind of file as a folder and want to retrive them as a list and after clicking any file from that list that file should get download I can do half of the way in normal class, I am making folder in project and doing this process its taking input from that folder and saving into other folder but when I am using servlet then its not working its not able to file in that folder its showing error.

Here is my code

public class main extends HttpServlet {
    private static String INPUTFILE = "Salary/pivot.pdf";

    public void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, java.io.IOException {
        try {
            Document document = new Document();
            PdfWriter writer = PdfWriter.getInstance(document,
                 new FileOutputStream("D:/test/ReadPdf.pdf"));
            document.open();

            PdfReader reader = new PdfReader(INPUTFILE);
            int n = reader.getNumberOfPages();
            PdfImportedPage page;

            for (int i = 1; i <= n; i++) {
                // only page number 2 will be included
                page = writer.getImportedPage(reader, i);
                Image instance = Image.getInstance(page);
                document.add(instance);
            }
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
} 

One more thing when I am removing that folder only giving file name as input and am keeping in project also putting file directly into web content its able to find and working but I want with folder.

Ambrish
  • 3,627
  • 2
  • 27
  • 42
sourav78611
  • 99
  • 2
  • 17

1 Answers1

1

Try to use servletContext.getRealPath method like this:

PdfReader reader = new PdfReader(getServletContext().getRealPath("/Salary/pivot.pdf"));

Check details in the Documentation

Gas
  • 17,601
  • 4
  • 46
  • 93
  • yea its working thanks is it possible i can show these files in list form and then after clicking particular file will get download... – sourav78611 Jul 22 '14 at 11:21
  • Yes, you could for example get path to the directory, then get all files using File API and create list with links to these files. – Gas Jul 22 '14 at 22:43
  • ok sir i will do can you give me little demo type any example using file API because i am not much strong just learning – sourav78611 Jul 23 '14 at 11:25
  • Please, try to use google before you ask - first item for example - http://stackoverflow.com/questions/5694385/getting-the-filenames-of-all-files-in-a-folder – Gas Jul 23 '14 at 11:39