1 | package web.servlets;
|
---|
2 |
|
---|
3 |
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.FileInputStream;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.InputStream;
|
---|
8 | import java.io.PrintWriter;
|
---|
9 | import javax.servlet.RequestDispatcher;
|
---|
10 | import javax.servlet.ServletException;
|
---|
11 | import javax.servlet.ServletOutputStream;
|
---|
12 | import javax.servlet.http.HttpServlet;
|
---|
13 | import javax.servlet.http.HttpServletRequest;
|
---|
14 | import javax.servlet.http.HttpServletResponse;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * @author University of Waikato - Te Whare WÄnanga o Waikato
|
---|
18 | * @version 1.0
|
---|
19 | * @since 2014-11-20
|
---|
20 | */
|
---|
21 | public class Download extends HttpServlet {
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
|
---|
25 | * @param request servlet request
|
---|
26 | * @param response servlet response
|
---|
27 | * @throws ServletException if a servlet-specific error occurs
|
---|
28 | * @throws IOException if an I/O error occurs
|
---|
29 | */
|
---|
30 | protected void processRequest(HttpServletRequest request, HttpServletResponse response)
|
---|
31 | throws ServletException, IOException {
|
---|
32 | response.setContentType("text/html;charset=UTF-8");
|
---|
33 | PrintWriter out = response.getWriter();
|
---|
34 | try {
|
---|
35 | out.println("<html>");
|
---|
36 | out.println("<head>");
|
---|
37 | out.println("<title>Servlet DownloadServlet</title>");
|
---|
38 | out.println("</head>");
|
---|
39 | out.println("<body>");
|
---|
40 | out.println("<h1>It appears you have accessed a place you shouldn't have.</h1>");
|
---|
41 | out.println("</body>");
|
---|
42 | out.println("</html>");
|
---|
43 | } finally {
|
---|
44 | out.close();
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
|
---|
49 | /**
|
---|
50 | * Handles the HTTP <code>GET</code> method.
|
---|
51 | * @param request servlet request
|
---|
52 | * @param response servlet response
|
---|
53 | * @throws ServletException if a servlet-specific error occurs
|
---|
54 | * @throws IOException if an I/O error occurs
|
---|
55 | */
|
---|
56 | @Override
|
---|
57 | protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
---|
58 | throws ServletException, IOException {
|
---|
59 | processRequest(request, response);
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Handles the HTTP <code>POST</code> method.
|
---|
64 | * @param request servlet request
|
---|
65 | * @param response servlet response
|
---|
66 | * @throws ServletException if a servlet-specific error occurs
|
---|
67 | * @throws IOException if an I/O error occurs
|
---|
68 | */
|
---|
69 | @Override
|
---|
70 | protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
---|
71 | throws ServletException, IOException {
|
---|
72 | File file = new File(request.getParameter("filepath"));
|
---|
73 | ServletOutputStream out = null;
|
---|
74 | InputStream in = null;
|
---|
75 | try {
|
---|
76 | response.setContentType("application/x-download");
|
---|
77 | response.setHeader("Content-Disposition", "attachment; filename=" + request.getParameter("filename"));
|
---|
78 | out = response.getOutputStream();
|
---|
79 | in = new FileInputStream(file);
|
---|
80 | byte[] bytes = new byte[1 * 1024 * 1024];
|
---|
81 | int bytesRead;
|
---|
82 | while ((bytesRead = in.read(bytes)) != -1) {
|
---|
83 | out.write(bytes, 0, bytesRead);
|
---|
84 | }
|
---|
85 | } catch (Exception e) {
|
---|
86 | e.printStackTrace();
|
---|
87 | } finally {
|
---|
88 | out.close();
|
---|
89 | in.close();
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Returns a short description of the servlet.
|
---|
95 | * @return a String containing servlet description
|
---|
96 | */
|
---|
97 | @Override
|
---|
98 | public String getServletInfo() {
|
---|
99 | return "Short description";
|
---|
100 | }// </editor-fold>
|
---|
101 | }
|
---|