Implementing File Download in a Web Page Using Hyperlinks and a Java Servlet
This article explains how to create a web page that enables downloading zip archives and images by first using simple hyperlink references and then implementing a Java Servlet that streams files with proper MIME types and Content‑Disposition headers, ensuring consistent download behavior across browsers.
Requirement: implement a web page that can download compressed packages and images.
Two implementation methods are described.
1: Download via Hyperlink
In an HTML page, link directly to the file paths, e.g., <a href="/day06/download/cors.zip">压缩包</a> for a zip file and <a href="/day06/download/1.png">图片</a> for an image. The following sample HTML demonstrates the approach:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>通过链接下载文件</h1>
<a href="/day06/download/cors.zip">压缩包</a>
<a href="/day06/download/1.png">图片</a>
</body>
</html>When the browser can recognize the file format (e.g., an image), it opens the file instead of prompting a download, which is why the image link displays the picture rather than downloading it.
2: Download via Servlet
The second method uses a Java Servlet to read the target file and write it to the response stream, forcing a download regardless of the file type. The servlet sets the appropriate MIME type and the Content‑Disposition header to attachment;filename=....
package com.lsgjzhuwei.servlet.response;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(asyncSupported = true, urlPatterns = { "/ServletDownload" })
public class ServletDownload extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String filename = request.getParameter("filename");
System.out.println(filename);
response.setContentType(getServletContext().getMimeType(filename));
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
String fullFileName = getServletContext().getRealPath("/download/" + filename);
InputStream in = new FileInputStream(fullFileName);
OutputStream out = response.getOutputStream();
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
out.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Not used
}
}After adding the servlet (mapped to /ServletDownload) and restarting the Tomcat server, links such as
<a href="/day06/ServletDownload?filename=cors.zip">压缩包</a>and
<a href="/day06/ServletDownload?filename=1.png">图片</a>trigger proper file downloads.
Thus, both zip archives and images can be downloaded reliably using either direct hyperlinks (when the browser does not natively display the file) or the servlet‑based approach for consistent behavior.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
