45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:JSP中上传下载的方法

JSP中上传下载的方法

2016-09-08 21:13:06 来源:www.45fan.com 【

JSP中上传下载的方法

在B/S系统开发中经常碰到一些文件上传下载的操作,一般公司可以根据自己的需要做成一个单独的组件,这样在需要上传下载功能的系统中直接加入即可。

在这里介绍的文件上传采用的是三方软件包commons-fileupoad.jar

当然也有其他的方式,可以根据需要选择。

一用fileupload实现上传

文件上传的实现可以采用三方包commons-fileupload.jar,具体使用情况可参考:

http://dev2dev.bea.com.cn/bbs/thread.jspa?forumID=121&threadID=8025&tstart=0&quint=true

二 将一个jsp中的表格导出到excel/word

很多时候需要从jsp(或xslt的查询结果)导出到excel或word,一个简单的方法是采用jsp实现,具体实现方式(以导出到excel为例)

first.jsp(此jsp用来显示查询结果,上面有一个按钮,点击下载):

<script language="javascript">

function doExport(){

document.all.form1.action="export.jsp";

var str = document.getElementById("table1").outerHTML;

document.all.excelText.value= str;

document.all.form1.submit();

}

<script>

<form name="form1" method="post" action="">

<input type="hidden" name="excelText" id="excelText" >

<input name="exportBtn" type="button" onclick="doExport()" class="button" value="导出">

</form>

<table width="100%" id="table1" border="1" cellpadding="2" cellspacing="1" bordercolordark="#FFFFFF">

.............................这里面是具体需要导出去的数据

</table>

export.jsp(执行导出操作)

<%@page contentType="text/html;charset=GB2312"%>

<%

String fileName="fileName";//随便定义,也可不定义

response.setContentType( "Application;charset=GB2312");

response.setHeader("Content-disposition","attachment;filename=/"" + fileName + "/";");

java.io.PrintWriter bos = response.getWriter();

String html = request.getParameter("excelText");

bos.write(html);

bos.close();

%>

到此,文件导出操作完成,点击“导出”按钮即可出现保存对话框。

在做的过程中偶然碰到了一个问题,点击一次导出,可以顺利保存,点击第二次时出现脚本错误。

解决方法是在doExport()方法中指定document.all.form1.target = "_blank";这样可以顺利保存,但打开了一个新窗口。

另见:

http://gocom.primeton.com/modules/newbb/item6387_6387.htm?PHPSESSID=954fcc1380ffdda1e535081d4c049529

 

 

本文地址:http://www.45fan.com/a/question/73774.html
Tags: 系统 JSP 开发
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部