使用Java servlet发送excel文件给客户端 [英] send excel file to client using java servlets

查看:216
本文介绍了使用Java servlet发送excel文件给客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的的Apache POI 的生成的 Excel中的文件中的 Java小

I am using Apache POI for generating Excel file in Java Servlets.

getExcel()函数返回 HSSFWorkbook ,我要发送到客户端。

getExcel() function returns HSSFWorkbook, which I want to send to the client.

HSSFWorkbook wb = getExcel();

这是我到目前为止已经试过。

This is what I have tried so far.

//block1
ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
wb.write(outByteStream);
byte [] outArray = outByteStream.toByteArray();
response.setContentType("application/ms-excel");
response.setContentLength(outArray.length);
response.setHeader("Expires:", "0");
response.setHeader("Content-Disposition", "attachment; filename=Demo1.xls");
OutputStream outStream = response.getOutputStream();
outStream.write(outArray);
outStream.flush();

//block2
request.setAttribute("Message", str1);
request.setAttribute("MessageDetails", str2);
request.getRequestDispatcher("/MyFile.jsp").forward(request, response);

以上code发送的excel文件到客户端,但给了我异常:

Above code sends excel file to the client, but gives me exception:

java.lang.IllegalStateException: Cannot forward after response has been committed

如果我删除块1 块2 从上面code,那么它不会给错误,但我想送客户端 Excel文件和我已经添加到两个属性要求对象。

If I remove the block1 or block2 from above code then it will not give error, but I want to send client Excel file and two attributes which I have added to request object.

所以,可以发送 Excel中使用文件至客户端的request.getRequestDispatcher ?或者是有这样做的更好的办法?

So can send Excel file to client using request.getRequestDispatcher ? Or is there any better way for doing this?

任何建议将AP preciated。

Any suggestion will be appreciated.

EDIT1 结果
我知道为什么我收到 IllegalStateException异常,但我的问题是我应该怎么发 ExcelFile 请求属性同时向客户端?

Edit1
I know why I am getting the IllegalStateException, but then my question is how should I send ExcelFile and Request Attributes both to the client?

EDIT2 结果
我为什么要同时发送 Excel文件的原因属性的客户端是 MyFile的的.jsp 有一个< D​​IV> 这将显示信息从发送的servlet

Edit2
The Reason why I want to send both Excel file and Attributes to the client is that MyFile.jsp has a <div> which will show message send from servlet.

<div style="background-color: aliceblue">
    <h3>${Message}</h3>
</div>

EDIT3 结果
我为什么要发送消息给客户端的原因是,我送这个 Excel文件导入Excel的操作的响应在客户端将提供 excel文件在数据库中插入数据,然后我突出了练成行哪些不能由于重复或任何其它原因而被插入。所以,我想显示在消息客户端导入的统计数据,并给他复制Excel中有错误行文件突出显示。

Edit3
The Reason why I want to send message to client is that I am sending this Excel file as an response to Import Excel operation in which client will provide excel file for inserting data in database, and then I am highlighting the excel rows which cannot be inserted due to duplication or any other reasons. So I want to show Import statistics in the Message to client and give him copy of excel file with error rows highlighted.

推荐答案

正在冲洗你的响应,然后试图转发。集装箱已经发了响应返回给客户端,现在是进退两难,如何转发请求另一个JSP,因此它中止运作方式中旬抛出异常。 HTTP是一个请求 - 响应模式。一旦你的要求的,你拿回的响应的。但一旦的响应的已经承诺在整个交易就结束了。

You are flushing your response and then trying to forward. Container has already sent the response back to the client and now is in a dilemma as to how to forward the request to another JSP, hence it aborts the operation mid way throwing an Exception. HTTP is a request-response model . Once you request , you get back a response . But once the response is already committed the whole transaction is over.

outStream.write(outArray); 
// you already committed the response here by flushing the output stream
outStream.flush(); 

//block2
request.setAttribute("Message", str1);
request.setAttribute("MessageDetails", str2);
// this is illegal after you have already flushed the response
request.getRequestDispatcher("/MyFile.jsp").forward(request, response);

由于每<一个href=\"http://docs.oracle.com/javaee/6/api/javax/servlet/RequestDispatcher.html#forward%28javax.servlet.ServletRequest,javax.servlet.ServletResponse%29\"相对=nofollow>的Javadoc :

IllegalStateException异常 - 如果响应已承诺

IllegalStateException - if the response was already committed.

EDIT1之后:

不,你不能两者都做。你需要决定你想要什么。写设置适当的HEADERS和MIME类型的字节的响应。你不能在浏览器下载的东西以及来自相同的响应表明一个JSP页面。

No you cannot do both . You need to decide what you want. Write the bytes to the response setting proper HEADERS and MIME-TYPE. You cannot get the browser download something as well as show a JSP page from the same response.

这篇关于使用Java servlet发送excel文件给客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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