简体   繁体   English

单击html中的锚点调用文件下载servlet?

[英]Calling file download servlet on click of anchor in html?

I wrote a file download servlet and registered in web.xml as below. 我编写了一个文件下载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>

FileDownloadServlet.java FileDownloadServlet.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(); 
   }
}

I have a link in html page as below. 我在html页面中有一个链接,如下所示。 on click of the link i need to get the file downloaded. 点击链接后,我需要下载文件。

<a class="button right" target="_blank">Download</a>

Now how can I link the download servlet with the anchor? 现在如何将下载servlet与锚链接起来?
Any suggestions? 有什么建议么?

It is not good practice to give the root mapping of the web application to do specific operations, therefore update your web.xml with proper servlet mapping as follows:- 让Web应用程序的根映射执行特定操作不是一个好习惯,因此请使用适当的servlet映射更新web.xml,如下所示:

  <servlet-mapping>
      <servlet-name>fileDownload</servlet-name>
      <url-pattern>/fileDownload</url-pattern>
 </servlet-mapping>

then update the anchor tag with new mapping as below. 然后使用以下新映射更新锚标记。 (you should mention the url-pattern insde the href attribute of the anchor tag.) (您应该提及锚标签的href属性的url-pattern。)

<a class="button right" target="_blank" href="/fileDownload">Download</a>

This should work!!! 这应该工作!!!

Give proper mapping in web.xml 在web.xml中提供适当的映射

  <servlet-mapping>

                <servlet-name>downloadFile</servlet-name>

                <url-pattern>/downloadFile</url-pattern>

        </servlet-mapping>

You just have to call the servlet through your anchor 您只需要通过锚点调用servlet即可

<a href="/downloadFile"  class="button right" target="_blank">Download</a>

Change your mapping to something specific: 将映射更改为特定的内容:

<servlet-mapping>
   <servlet-name>downloadFile</servlet-name>
   <url-pattern>/download</url-pattern>
</servlet-mapping>

And specify the href attribute of a tag : 并指定href的属性, a标签:

<a class="button right" target="_blank" href="/download">Download</a>

You can read the specs of the anchor#href tag : 您可以阅读anchor#href标签的规范:

This attribute specifies the location of a Web resource, thus defining a link between the current element (the source anchor) and the destination anchor defined by this attribute. 此属性指定Web资源的位置,从而定义此属性定义的当前元素(源锚点)和目标锚点之间的链接。

在所有三个答案中,所有内容都是正确的,除了href =“ / downloadFile”省略'/'来产生-href =“ downloadFile”为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM