import org.apache.commons.io.IOUtils;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Download extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = getServletContext();
InputStream resourceAsStream = servletContext.getResourceAsStream("/file/aaa.docx");
String mimeType = servletContext.getMimeType("/file/aaa.docx");
System.out.println(" :" + mimeType);
resp.setContentType(mimeType);
OutputStream outputStream = resp.getOutputStream();
IOUtils.copy(resourceAsStream,outputStream);
resp.setHeader("Content-Disposition","attachment;filename= .docx");
}
}
<servlet>
<servlet-name>Downloadservlet-name>
<servlet-class>ProjectTest.Downloadservlet-class>
servlet>
<servlet-mapping>
<servlet-name>Downloadservlet-name>
<url-pattern>/downloadurl-pattern>
servlet-mapping>