简体   繁体   English

如何使JSP页面下载2个文件

[英]How to make a jsp page download 2 files

I have a download.jsp page that when loaded, causes a file to download using the following code: 我有一个download.jsp页面,该页面在加载后会导致使用以下代码下载文件:

String contentDisp = "attachment; filename=file_" + "."+DeptNumber+ ".txt";     
response.setContentType("text/plain");
response.setHeader("Content-Disposition", contentDisp);

After this, i do some out.write(....) statements and then in the end out.flush() which is when the user receives a download file request from the browser. 之后,我执行一些out.write(....)语句,最后执行out.flush(),这是用户从浏览器收到下载文件请求的时间。 After that i use: 之后,我使用:

response.sendRedirect("landingpage.jsp");

To move the user on to the next page. 将用户移至下一页。

Now, I want to do the same but instead of one file, i want the page to cause 2 files to be generated for download. 现在,我想做同样的事情,但是我想让页面生成2个文件以供下载,而不是一个文件。 I did 2 changes: 我做了2处更改:

1) I changed the beginning of the jsp to check for an attribute, and according to the attribute sent, generate the correct file for download. 1)我更改了jsp的开头以检查属性,并根据发送的属性生成正确的文件以进行下载。

String downloadDeptNumber = request.getAttribute("dept")==null ? "1" : request.getAttribute("dept").toString();

and

2) In the end of the page instead of redirecting to the next page, i check if the attribute was 1, i change it to 2 and redirect to the same page. 2)在页面末尾,而不是重定向到下一页,我检查属性是否为1,将其更改为2并重定向到同一页面。 If it wasn't 1, i redirect to the end page (landing page) that i used originally with only 1 download above. 如果不是1,我将重定向到我最初仅在上面下载1个文件时使用的最终页面(登录页面)。

if (downloadDeptNumber.equals("1")) 
{
      redirectUrl="download.jsp" ;
      session.setAttribute("dept", "2");
}
else
{
      redirectUrl= "landingpage.jsp";
      session.removeAttribute("dept");
}
response.sendRedirect(redirectUrl);

But it seems i can't redirect to the same page i am on currently. 但似乎我无法重定向到当前所在的同一页面。

Does anyone have a solution for me ? 有人对我有解决方案吗?

Thanks. 谢谢。

option # 1 : separate link 选项#1:单独的链接

Give 2 separate link to download each file 提供2个单独的链接以下载每个文件

option #2 : zip file 选项#2 :zip文件

make a single zip file on server using java API and download 使用Java API在服务器上制作一个zip文件并下载

option #3 : Use java script 选项#3 :使用Java脚本

make a java script function that will open the new tab and download the each file simultaneously 制作一个Java脚本函数,该函数将打开新标签并同时下载每个文件

Below code should run to download each file 下面的代码应运行以下载每个文件

window.open(
  'http://download_file_link',
  '_blank' // <- This is what makes it open in a new window.
);

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

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