2
0

Download.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.IOException;
  9. import java.io.OutputStream;
  10. import java.io.PrintWriter;
  11. import javax.servlet.ServletContext;
  12. import javax.servlet.ServletException;
  13. import javax.servlet.annotation.WebServlet;
  14. import javax.servlet.http.HttpServlet;
  15. import javax.servlet.http.HttpServletRequest;
  16. import javax.servlet.http.HttpServletResponse;
  17. /**
  18. *
  19. * @author Deben
  20. */
  21. @WebServlet(urlPatterns = {"/Download"})
  22. public class Download extends HttpServlet {
  23. /**
  24. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  25. * methods.
  26. *
  27. * @param request servlet request
  28. * @param response servlet response
  29. * @throws ServletException if a servlet-specific error occurs
  30. * @throws IOException if an I/O error occurs
  31. */
  32. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  33. /**
  34. * Handles the HTTP <code>GET</code> method.
  35. *
  36. * @param request servlet request
  37. * @param response servlet response
  38. * @throws ServletException if a servlet-specific error occurs
  39. * @throws IOException if an I/O error occurs
  40. */
  41. @Override
  42. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  43. throws ServletException, IOException {
  44. String device = request.getParameter("device");
  45. String relativePath;
  46. switch(device) {
  47. case "android":
  48. relativePath = getServletContext().getRealPath("/WEB-INF/android/Access control.apk");
  49. break;
  50. case "ios":
  51. relativePath = getServletContext().getRealPath("/WEB-INF/");
  52. break;
  53. default:
  54. return;
  55. }
  56. // reads input file from an absolute path
  57. File downloadFile = new File(relativePath);
  58. FileInputStream inStream = new FileInputStream(downloadFile);
  59. // if you want to use a relative path to context root:
  60. System.out.println("relativePath = " + relativePath);
  61. // obtains ServletContext
  62. ServletContext context = getServletContext();
  63. // gets MIME type of the file
  64. String mimeType = context.getMimeType(relativePath);
  65. if (mimeType == null) {
  66. // set to binary type if MIME mapping not found
  67. mimeType = "application/octet-stream";
  68. }
  69. System.out.println("MIME type: " + mimeType);
  70. // modifies response
  71. response.setContentType(mimeType);
  72. response.setContentLength((int) downloadFile.length());
  73. // forces download
  74. String headerKey = "Content-Disposition";
  75. String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
  76. response.setHeader(headerKey, headerValue);
  77. // obtains response's output stream
  78. OutputStream outStream = response.getOutputStream();
  79. byte[] buffer = new byte[4096];
  80. int bytesRead = -1;
  81. while ((bytesRead = inStream.read(buffer)) != -1) {
  82. outStream.write(buffer, 0, bytesRead);
  83. }
  84. inStream.close();
  85. outStream.close();
  86. }
  87. /**
  88. * Handles the HTTP <code>POST</code> method.
  89. *
  90. * @param request servlet request
  91. * @param response servlet response
  92. * @throws ServletException if a servlet-specific error occurs
  93. * @throws IOException if an I/O error occurs
  94. */
  95. @Override
  96. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  97. throws ServletException, IOException {
  98. }
  99. }