我怎样才能得到用户下载我的档案? (Java的,MVC,Excel中,POI) [英] How can I get the user to download my file? (Java, MVC, Excel, POI)
问题描述
我使用的Struts2,Spring和Hibernate的。
我已经写了叫TEST.XLS与POI一个简单的Excel文件。当我运行的动作,它的工作原理是因为TEST.XLS出现,但在我的tomcat的文件夹中。
我想这个文件被我的JSP页面上下载。我该如何去获得用户下载.xls文件?相反,我怎么叫这条道路?
很抱歉,如果答案是显而易见的,我是新来这个。我知道它可能有一些做的ServletContext的或HttpServletRequest的?
我得到它的工作多亏了下面的答案。这里是工作code:
私人的InputStream excelStream;公共字符串出口(){
//创建excelfile
HSSFWorkbook工作簿=新HSSFWorkbook();
的FileOutputStream文件= NULL; 尝试{
文件=新的FileOutputStream(POI-TEST.XLS);
}
赶上(IOException异常五){
e.printStackTrace();
}
//填充工作簿的所有的Excel的东西
...//写入文件
ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
尝试{
workbook.write(文件);
workbook.write(BAOS);
ByteArrayInputStream的双=新ByteArrayInputStream进行(baos.toByteArray());
excelStream =二;
}
赶上(IOException异常五){
e.printStackTrace();
}
返回创先争优
}公众的InputStream getExcelStream(){
返回excelStream;
}
公共无效setExcelStream(InputStream的excelStream){
this.excelStream = excelStream;
}
在我的支柱文件:
<结果名称=创先争优类型=流>
< PARAM NAME =的contentType>应用/ vnd.ms-的Excel< /参数>
< PARAM NAME =inputName> excelStream< /参数>
< PARAM NAME =contentDisposition>附件;文件名=excelExport.xls< /参数>
< PARAM NAME =缓冲区大小> 1024 LT; /参数>
< /&结果GT;
您要使用的Struts2的流结果这一点。下面是一些信息:
I'm using Struts2, Spring, and Hibernate.
I have written a simple Excel file called test.xls with POI. When I run the action, it works because test.xls appears, but in my tomcat folder.
I want that file to be downloadable on my jsp page. How do I go about getting the user to download the .xls file? Rather, how do I call that path?
Sorry if the answer is obvious, I am new to this. I know it probably has something to do with the ServletContext or HttpServletRequest?
[edit] I got it working thanks to the below answers. Here is the working code:
private InputStream excelStream;
public String export(){
//Create excelfile
HSSFWorkbook workbook = new HSSFWorkbook();
FileOutputStream file = null;
try{
file = new FileOutputStream("poi-test.xls");
}
catch(IOException e){
e.printStackTrace();
}
//Populate workbook with all the excel stuff
...
//Write to file
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try{
workbook.write(file);
workbook.write(baos);
ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
excelStream = bis;
}
catch(IOException e){
e.printStackTrace();
}
return "excel";
}
public InputStream getExcelStream() {
return excelStream;
}
public void setExcelStream(InputStream excelStream) {
this.excelStream = excelStream;
}
Over in my struts file:
<result name="excel" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="inputName">excelStream</param>
<param name="contentDisposition">attachment; filename="excelExport.xls"</param>
<param name="bufferSize">1024</param>
</result>
You want to use the Struts2 Stream Result for this. Here is some information on that:
- http://struts.apache.org/2.2.1/docs/ajax.html
- how to return excel in Struts2 result?
- Using Stream Result with Struts2
这篇关于我怎样才能得到用户下载我的档案? (Java的,MVC,Excel中,POI)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!