Tomcat install directory\conf\web.xml
if (mimeType == null) {
mimeType = "application/octet-stream";
} response.setHeader("Content-Disposition", "attachment; filename=\"MyFile.mp4\""); And following is code of the Java servlet called DownloadFileServlet:package net.codejava;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DownloadFileServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// reads input file from an absolute path
String filePath = "E:/Test/Download/MYPIC.JPG";
File downloadFile = new File(filePath);
FileInputStream inStream = new FileInputStream(downloadFile);
// if you want to use a relative path to context root:
String relativePath = getServletContext().getRealPath("");
System.out.println("relativePath = " + relativePath);
// obtains ServletContext
ServletContext context = getServletContext();
// gets MIME type of the file
String mimeType = context.getMimeType(filePath);
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();
}
}Configure URL mapping for this servlet in the web deployment descriptor file web.xml as follows:<servlet> <description>This servlet sends file to client</description> <display-name>DownloadFileServlet</display-name> <servlet-name>DownloadFileServlet</servlet-name> <servlet-class>net.codejava.DownloadFileServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>DownloadFileServlet</servlet-name> <url-pattern>/DownloadFileServlet</url-pattern> </servlet-mapping>Or use the @WebServlet annotation in Servlet 3.0:
@WebServlet("/DownloadFileServlet")
public class DownloadFileServlet extends HttpServlet {
// code of the servlet...
} We can invoke the servlet in the following form of URL:http://localhost:8080/MyWebApp/DownloadFileServlet
The browser should ask the user to download the file as shown in the following screenshot:
Related Java File Dowload Tutorials:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.