简体   繁体   English

使用JSP创建CSV文件时出错(Tomcat 7 / Java)

[英]Error when creating CSV-File with JSP (Tomcat 7/Java)

I have an JSP-File, which creates a CSV-File. 我有一个JSP文件,它创建一个CSV文件。

To tell the server that the file is an CSV-File I added the following code to the JSP: 为了告诉服务器该文件是CSV文件,我在JSP中添加了以下代码:

response.setContentType ("application/csv");
response.setHeader("Content-Disposition", "attachment;filename=test.csv"); 

In the file I simply write the content of the file with an "Writer" object (the out-Variable). 在文件中,我只是用“ Writer”对象(out-Variable)写文件的内容。 Therefore I use the following code to gernerate the file: 因此,我使用以下代码来整理文件:

out.write("test1;test2;etc");

There are about 16 columns for this csv-file with about 3000 rows. 此csv文件大约有16列,大约有3000行。

The Problem I have is that the "Download file"-Dialog does not appear with bigger files. 我遇到的问题是“下载文件”对话框没有显示较大的文件。 When I only have 128 rows it works and the Browser does show the "Download File"-windows. 当我只有128行时,它可以工作,并且浏览器确实显示“下载文件”窗口。 When I have 129 rows the browser simply opens the file with plain text (with the IE ignoring the line breaks). 当我有129行时,浏览器仅用纯文本(IE忽略换行符)打开文件。 This happens in every browser I tested (IE 11 and Firefox). 在我测试过的每种浏览器(IE 11和Firefox)中都会发生这种情况。 I actually have no idea why this happens, though my guess is that it has something to do with the Writer-Object. 我实际上不知道为什么会这样,尽管我猜想这与Writer-Object有关。 I already tried to "flush()" the Writer, but this doesn't change anything. 我已经尝试过“刷新()”编写器,但这并没有改变任何内容。

Thanks in advance! 提前致谢!

Edit: I did strip down the code to the following. 编辑:我确实将代码简化为以下内容。 When j is limited to 50 it does work. 当j限制为50时,它将起作用。 With is being set to 100, it doesn't. 与设置为100时没有。 The .jsp is open with href.location or window.open() (it does not seem to make a difference) .jsp使用href.location或window.open()打开(似乎没有什么不同)

<%
    int j = 0;
    int k = 0;

    for (j = 0; j < 100; j++)
    {

        out.write(";");

        for (k = 0; k < 16; k++)
        {
            out.write(j + "TEST");

            if (k != 15)
            {
                out.write(";");
            }
        }

        out.write("\r\n");
    }


    response.setContentType ("application/csv");
    response.setHeader("Content-Disposition", "attachment;filename=test.csv"); 
%>

Change below configurations in your tomcat webapps/manager/WEB-INF/web.xml . tomcat webapps / manager / WEB-INF / web.xml中更改以下配置。 we can change this setting , just put 0 on each size limit. 我们可以更改此设置,只需在每个大小限制上输入0。 the finally configure : 最后配置:

  <multipart-config>
       <!-- 50MB max -->
       <max-file-size>524288000</max-file-size>
        <max-request-size>524288000</max-request-size>
        <file-size-threshold>0</file-size-threshold>
     </multipart-config>

Also check for , 同时检查,

maxPostSize maxPostSize

The maximum size in bytes of the POST which will be handled by the container FORM URL parameter parsing. 容器FORM URL参数解析将处理的POST的最大大小(以字节为单位)。 The limit can be disabled by setting this attribute to a value less than or equal to 0. If not specified, this attribute is set to 2097152 (2 megabytes) . 可以通过将此属性设置为小于或等于0的值来禁用该限制。如果未指定,则将该属性设置为2097152 (2 megabytes)

Another Limit is: 另一个限制是:

maxHttpHeaderSize The maximum size of the request and response HTTP header, specified in bytes. maxHttpHeaderSize请求和响应HTTP标头的最大大小,以字节为单位。 If not specified, this attribute is set to 4096 (4 KB) . 如果未指定,则此属性设置为4096 (4 KB)

You find them in 您在中找到它们

$TOMCAT_HOME/conf/server.xml

Jsp are great to produce HTML output. Jsp非常适合产生HTML输出。

As you want to produce csv, you'd better use a servlet. 当您要生成csv时,最好使用servlet。 I did it and it works well with 50000 rows. 我做到了,它与50000行一起很好地工作。 Here is my demo code : 这是我的演示代码:

public class CsvServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest hsr, HttpServletResponse response) throws ServletException, IOException {
        response.setStatus(HttpServletResponse.SC_OK);
        response.setHeader("Content-Disposition", "attachment;filename=test.csv");
        response.setContentType ("application/csv");
        PrintWriter pw = response.getWriter();
        for (int i=0; i<50000; i++) {
            pw.print(i);
            for (char c: "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray()) {
                pw.print(',');
                pw.print(c);
            }
            pw.println();
        }
    }

}

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

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