If the file is dynamic i.e. a JSP file then:
//in the html file
<a href="download.jsp">download the jsp file</a>
// the download.jsp file
Source: javatpoint https://www.javatpoint.com/downloading-file-from-the-server-in-jsp
But if the file is static e.g. a txt file the:
It can be done in a much simpler way :
//in the html file
<a href="download.jsp">download the jsp file</a>
// the download.jsp file
- <%
- String filename = "home.jsp";
- String filepath = "e:\\";
- response.setContentType("APPLICATION/OCTET-STREAM");
- response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
- java.io.FileInputStream fileInputStream=new java.io.FileInputStream(filepath + filename);
- int i;
- while ((i=fileInputStream.read()) != -1) {
- out.write(i);
- }
- fileInputStream.close();
- %>
Source: javatpoint https://www.javatpoint.com/downloading-file-from-the-server-in-jsp
But if the file is static e.g. a txt file the:
It can be done in a much simpler way :
If the resource is static, just put it in the public webcontent (there where your JSP/HTML/CSS/JS/etc files also are) and include a link to it in your JSP.
<a href="file.txt">download</a>
Source: stackoverflow https://stackoverflow.com/questions/4005873/using-jsp-to-download-a-file
No comments:
Post a Comment