| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- *
- * @author Deben
- */
- @WebServlet(urlPatterns = {"/Download"})
- public class Download extends HttpServlet {
- /**
- * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
- * methods.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
-
- // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
- /**
- * Handles the HTTP <code>GET</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String device = request.getParameter("device");
- String relativePath;
- switch(device) {
- case "android":
- relativePath = getServletContext().getRealPath("/WEB-INF/android/Access control.apk");
- break;
- case "ios":
- relativePath = getServletContext().getRealPath("/WEB-INF/");
- break;
- default:
-
- return;
- }
- // reads input file from an absolute path
-
- File downloadFile = new File(relativePath);
- FileInputStream inStream = new FileInputStream(downloadFile);
-
- // if you want to use a relative path to context root:
-
- System.out.println("relativePath = " + relativePath);
-
- // obtains ServletContext
- ServletContext context = getServletContext();
-
- // gets MIME type of the file
- String mimeType = context.getMimeType(relativePath);
- if (mimeType == null) {
- // set to binary type if MIME mapping not found
- mimeType = "application/octet-stream";
- }
- System.out.println("MIME type: " + mimeType);
-
- // modifies response
- response.setContentType(mimeType);
- response.setContentLength((int) downloadFile.length());
-
- // forces download
- String headerKey = "Content-Disposition";
- String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
- response.setHeader(headerKey, headerValue);
-
- // obtains response's output stream
- OutputStream outStream = response.getOutputStream();
-
- byte[] buffer = new byte[4096];
- int bytesRead = -1;
-
- while ((bytesRead = inStream.read(buffer)) != -1) {
- outStream.write(buffer, 0, bytesRead);
- }
-
- inStream.close();
- outStream.close();
- }
- /**
- * Handles the HTTP <code>POST</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- }
- }
|