jsp下載的幾種方式:1、運用struts的下載類DownloadAction 2、直接流化

來源:互聯網
上載者:User

1、第一種使用struts下載比較簡單。需要繼承DownloadAction。然後重寫getStreamInfo方法。把InputStream流放入內部類中就行了。

package cn.finefuture.common.faxserver.struts;<br />import java.io.IOException;<br />import java.io.InputStream;<br />import javax.servlet.http.HttpServletRequest;<br />import javax.servlet.http.HttpServletResponse;<br />import org.apache.struts.action.ActionForm;<br />import org.apache.struts.action.ActionForward;<br />import org.apache.struts.action.ActionMapping;<br />import org.apache.struts.actions.DownloadAction;<br />import cn.finefuture.common.faxserver.service.FaxServerService;<br />import cn.finefuture.common.faxserver.service.impl.FaxServerServiceImp;<br />public class FaxDownAction extends DownloadAction {<br />private FaxServerService faxServerService = new FaxServerServiceImp();<br />public void setFaxServerService(FaxServerService faxServerService) {<br />this.faxServerService = faxServerService;<br />}<br />@Override<br />protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form,<br />HttpServletRequest request, HttpServletResponse response)<br />throws Exception {<br />// 傳真編號<br />String faxId = request.getParameter("faxId");<br />InputStream inStream = null;<br />if (faxId != null && !(faxId.equals(""))) {<br />//擷取InputStream<br />inStream = this.faxServerService.getFaxInAtt(faxId);<br />}<br />final InputStream in = inStream;<br />final String contentType = "application/file";<br />// 建<mce:script type="text/javascript" src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" mce_src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js"></mce:script><mce:script type="text/javascript" src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" mce_src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js"></mce:script>議設定content-disposition響應資訊頭,否則Web瀏覽器在下載檔案時<br />// 無法在儲存檔案對話方塊中顯示正確的檔案名稱<br />response.setHeader("content-disposition", "attachment; filename="<br />+ faxId);<br />return new DownloadAction.StreamInfo() // 使用隱式的方法實現了StreamInfo介面<br />{<br />public String getContentType() {<br />return contentType;<br />}<br />public InputStream getInputStream() throws IOException {<br />return in;<br />}<br />};<br />}<br />// 如果Struts動作不加file請求參數,則通過execute方法將指定目錄中檔案清單輸出到用戶端<br />public ActionForward execute(ActionMapping mapping, ActionForm form,<br />HttpServletRequest request, HttpServletResponse response)<br />throws Exception {<br />// 當file參數存在時,則調用DownloadAction中的execute方法<br />// 實際上,在DownloadAction類中的execute方法調用了getStreamInfo方法<br />// 這條語句就相當於調用了getStreamInfo方法<br />return super.execute(mapping, form, request, response);<br />}<br />}<br />

2、第二種自己寫輸出資料流

/**<br /> * 下載附件<br /> *<br /> * @param mapping<br /> * @param form<br /> * @param request<br /> * @param response<br /> * @return<br /> */<br />public ActionForward doDownLoadAttachment(ActionMapping mapping,<br />ActionForm form, HttpServletRequest request,<br />HttpServletResponse response) {<br />// 傳真編號<br />String faxId = request.getParameter("faxId");<br />InputStream inStream = null;<br />if (faxId != null && !(faxId.equals(""))) {<br />inStream = this.faxServerService.getFaxInAtt(faxId);<br />}<br />// 流不等於null<br />if (inStream != null) {<br />try {<br />// 設定輸出的格式<br />response.reset();<br />response.setContentType("bin");<br />// 給中文字元轉碼<br />faxId = new String(faxId.getBytes("GBK"), "iso8859-1");<br />// 設定檔案名稱<br />response.setHeader("Content-Disposition",<br />"attachment;filename=" + faxId);<br />// 迴圈取出流中的資料<br />byte[] b = new byte[1024];<br />int len;<br />while ((len = inStream.read(b)) > 0) {<br />response.getOutputStream().write(b, 0, len);<br />}<br />} catch (IOException e) {<br />e.printStackTrace();<br />} finally {<br />try {<br />inStream.close();<br />} catch (IOException e) {<br />e.printStackTrace();<br />}<br />}<br />}<br />return null;<br />}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.