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 />}