我编写了一个文件下载 servlet 并在 web.xml 中注册,如下所示。
<servlet>
<servlet-name>downloadFile</servlet-name>
<servlet-class>com.hibu.HibuProspector.FileDwonloadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>downloadFile</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
文件下载Servlet.java
public class FileDownloadServlet extends HttpServlet{
private static final int BYTES_DOWNLOAD = 1024;
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException{
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition",
"attachment;filename=SampleFile.xlsx");
ServletContext ctx = getServletContext();
InputStream is = ctx.getResourceAsStream("/SampleFile.xlsx");
int read=0;
byte[] bytes = new byte[BYTES_DOWNLOAD];
OutputStream os = response.getOutputStream();
while((read = is.read(bytes))!= -1){
os.write(bytes, 0, read);
}
os.flush();
os.close();
}
}
我在 html 页面中有一个链接,如下所示。单击链接我需要下载文件。
<a class="button right" target="_blank">Download</a>
现在如何将下载 servlet 与锚链接?
有什么建议么?