Servlet redirect to a file download with name changed
How to redirect a file download with a changed file name?
In this Servlet Class, I put a function named getContentType to get the file type's name. It is required that the mimeType is set when redirect to a file download.
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:\\web\\ftp_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> } |
Once it is implemented, you can use the following link to redirect the address to a file download:
http://localhost:8080/FileManagement/FileServlet?filename=somefilename&realname=realfilename
<pre><code> String foo = "bar"; </code></pre>