Frage

I have a application in which I want to open a JSP file which is opened and created at runtime. My problem is that how to open it if I don't have fixed path of it. How do I know where it is and how to open it at runtime?

War es hilfreich?

Lösung

you can use servlet.

@WebServlet("/Test.pdf")
public class PdfServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
File file = new File("D:\\Test\\Test.pdf");
response.setHeader("Content-Type",    getServletContext().getMimeType(file.getName()));
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "inline; filename=\"Test.pdf\"");
     Files.copy(file.toPath(), response.getOutputStream());
}
}

(if Servlet 3.0 is not available, then map it in web.xml the usual way, if Java 7 is not available, then use a read/write loop the usual way)

Just copypaste this class in its entirety into your project and open the desired PDF file by /contextpath/Test.pdf instead of /contextpath/youroriginal.jsp (after having organized it in a package and autocompleted the necessary imports in the class, of course).

E.g. as follows in a JSP where you'd like to show the PDF inline:

<object data="${pageContext.request.contextPath}/Test.pdf" 
type="application/pdf" width="500" height="300">
<a href="${pageContext.request.contextPath}/Test.pdf">Download file.pdf</a>

Andere Tipps

to solve your problem Re-Write the link line as

<A HREF="<%="file://D:/filesDir/"+fileNames[i] %>"><%= fileNames[i]%></A>

but if you really want the files to be accessed on other systems as well other than the server itself, you should move your file into your web directory and then use relative path for access

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top