¿Cómo redirigir la descarga de un archivo con un nombre de archivo cambiado?
En esta clase de servlet, puse una función llamada getContentType para obtener el nombre del tipo de archivo. Es necesario que mimeType esté configurado cuando se redirija a la descarga de un archivo.
public class FileServlet extends HttpServlet { String getContentType(String fileName) { String extension[] = { // File Extensions "txt", //0 - plain text "htm", //1 - hypertext "jpg", //2 - JPEG image "png", //2 - JPEG image "gif", //3 - gif image "pdf", //4 - adobe pdf "doc", //5 - Microsoft Word "docx", }; // you can add more String mimeType[] = { // mime types "text/plain", //0 - plain text "text/html", //1 - hypertext "image/jpg", //2 - image "image/jpg", //2 - image "image/gif", //3 - image "application/pdf", //4 - Adobe pdf "application/msword", //5 - Microsoft Word "application/msword", //5 - Microsoft Word }, // you can add more contentType = "text/html"; // default type // dot + file extension int dotPosition = fileName.lastIndexOf('.'); // get file extension String fileExtension = fileName.substring(dotPosition + 1); // match mime type to extension for (int index = 0; index < mimeType.length; index++) { if (fileExtension.equalsIgnoreCase( extension[index])) { contentType = mimeType[index]; break; } } return contentType; } protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String filename = request.getParameter("filename"); String realname = request.getParameter("realname"); String path = "C:webftp_file"; String fullPath = path + filename; File file = new File(fullPath); String contentType = getContentType(filename); System.out.println(contentType); response.setContentType(contentType); response.setHeader("Content-Disposition", "attachment; filename=" + realname); int length = (int) file.length(); if (length > Integer.MAX_VALUE) { } byte[] bytes = new byte[length]; FileInputStream fin = new FileInputStream(file); fin.read(bytes); ServletOutputStream os = response.getOutputStream(); os.write(bytes); os.flush(); } // <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 { processRequest(request, response); } /** * 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 { processRequest(request, response); } /** * Returns a short description of the servlet. * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold> } |
Una vez implementado, puede utilizar el siguiente enlace para redirigir la dirección a una descarga de archivo:
http: // localhost: 8080 / FileManagement / FileServlet? filename = somefilename & realname = realfilename