There is a JSP page necessary to make a link to download the file. How can this be implemented?
1 answer
First you need to create a servlet to download the file. For example:
@WebServlet("/download") public class DownloadServlet extends HttpServlet { ... } Now describe the doGet( HttpServletRequest request, HttpServletResponse response) method doGet( HttpServletRequest request, HttpServletResponse response) :
// тип данных, которые вы отправляете // например application/pdf, text/plain, text/html, image/jpg response.setContentType("ТИП_ДАННЫХ_MIME"); response.setHeader("Content-disposition","attachment; filename=ВАШЕ_КАСТОМНОЕ_ИМЯ_ФАЙЛА.ext"); // файл, который вы отправляете File my_file = new File("ИМЯ_ФАЙЛА"); // отправить файл в response OutputStream out = response.getOutputStream(); FileInputStream in = new FileInputStream(my_file); byte[] buffer = new byte[4096]; int length; while ((length = in.read(buffer)) > 0){ out.write(buffer, 0, length); } // освободить ресурсы in.close(); out.flush(); This is a very simple file upload example.
Now in example.jsp you just need to place the link, for example
<a href="/download">скачать файл</a>
|