如何将文件下载到JSP文件中进行响应 [英] How to write a file download to response in JSP file

查看:131
本文介绍了如何将文件下载到JSP文件中进行响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从响应对象获取一个ServletOutputStream对象时,我得到一个java.lang.IllegalStateException。以下是我的代码:

I'm getting a java.lang.IllegalStateException when I try to get a ServletOutputStream object from the response object. Below is my code:

<%@ page import="java.util.*,java.io.*"%>             

<%
try {
    System.out.print("request came");
    File f = new File ("E:/dd.txt");

    String name = f.getName().substring(f.getName().lastIndexOf("/") + 1,f.getName().length());
    InputStream in = new FileInputStream(f);

    ServletOutputStream outs = response.getOutputStream();

    response.setContentType ("application/txt");
    response.setHeader ("Content-Disposition", "attachment; filename="+f.getName()+"");
    int bit = 256;
    int i = 0;
    try {
        while ((bit) >= 0) {
            bit = in.read();
            outs.write(bit);
        }
    } catch (IOException ioe) {
        ioe.printStackTrace(System.out);
    }
    outs.flush();
    outs.close();
    in.close();         
} catch (Exception ioe) {
    ioe.printStackTrace(System.out);
}
%>

以下是stacktrace:

The following is the stacktrace:

java.lang.IllegalStateException
   at org.apache.jasper.runtime.ServletResponseWrapperInclude.getOutputStream(ServletResponseWrapperInclude.java:63)
   at org.apache.jsp.html.portlet.vitage.custom.QUADWAVE.Procfiledownloadess1_005f36901_005f48.filedownload.downloadscreen_jsp._jspService(downloadscreen_jsp.java:5
   at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
   at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
   at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
   at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


推荐答案

您正在尝试下载一个文件b在JSP文件中有一些代码。 JSP作为视图技术实际上是错误的工具。 <%%> (通常是基于文本的内容,如HTML,XML,JSON等)之外的所有内容也被写入HTTP响应,包括空白即可。这只会破坏由Java代码编写的下载内容的完整性,如果您正在提供诸如文档/音频/视频文件的二进制文件,则更为如此。

You're attempting to download a file by some code in a JSP file. JSP as being a view technology is actually the wrong tool for the job. Everything outside <% %> (which is normally text based content like HTML, XML, JSON, etc) is written to the HTTP response as well, including whitespace. This would only corrupt the integrity of the downloaded content which is written by Java code, even more so if you're serving binary files such as document/audio/video files.

您的具体问题是由于JSP内部使用 response.getWriter()来打印所有模板内容(<%%> / code>),然后您尝试使用 getOutputStream()。这是非法的状态。您不能同时在单个响应中使用它们。除了使用 getWriter()之外,您可以通过删除<%%>之外的任何空格来解决此问题。 / code>,包括换行符。

Your concrete problem is caused because JSP internally uses response.getWriter() to print all the template content (everything outside <% %>) and then you're attempting to use getOutputStream(). This is an illegal state. You can't use them both simultaneously on a single response. Apart from using getWriter() instead, you could solve it by removing any whitespace outside <% %>, including newlines.

所以,替换

<%@ page import="java.util.*,java.io.*"%>             

<%
    // Your Java code.
%>

by

<%@ page import="java.util.*,java.io.*"%><%
    // Your Java code.
%>

(并确保在最后一个<$ c之后没有尾随空格/换行符$ c>%>

但是,您实际上不应该使用JSP作业。这是工作的错误工具。您应该使用正常的 HTTP servlet类作为该作业。只需创建一个扩展名为 HttpServlet 的类,并将您在JSP中的所有Java代码移动到 doGet()方法。最后在URL上映射该servlet,并调用该URL。

However, you should actually not be using JSP for the job. It's as said the wrong tool for the job. You should be using a normal HTTP servlet class for the job. Just create a class extending HttpServlet and move all the Java code which you've there in the JSP into the doGet() method. Finally map that servlet on an URL and invoke that URL instead.

@WebServlet("/download")
public class DownloadServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Your Java code.
    }

}

您可以找到一个更具体的例子在这篇文章

You can find a more concrete example in this article.

  • How to avoid Java code in JSP files?
  • Simplest way to serve static data from outside the application server in a Java web application
  • How to stream audio/video files such as MP3, MP4, AVI, etc using a Servlet

这篇关于如何将文件下载到JSP文件中进行响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆