Posted on 2006-07-10 16:40
負人博客 閱讀(405)
評論(0) 編輯 收藏 所屬分類:
JAVA技術
一??? 文件上傳
????????文件上傳的實現可以采用三方包commons-fileupload.jar,具體使用情況可參考:
????????http://dev2dev.bea.com.cn/bbs/thread.jspa?forumID=121&threadID=8025&tstart=0&quint=true
二??? 文件下載
???????? 很多時候需要從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(執(zhí)行導出操作)
<%@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
??
?