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

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

package cn.finefuture.common.faxserver.struts;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;
import cn.finefuture.common.faxserver.service.FaxServerService;
import cn.finefuture.common.faxserver.service.impl.FaxServerServiceImp;
public class FaxDownAction extends DownloadAction {
	private FaxServerService faxServerService = new FaxServerServiceImp();
	public void setFaxServerService(FaxServerService faxServerService) {
		this.faxServerService = faxServerService;
	}
	@Override
	protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 傳真編號
		String faxId = request.getParameter("faxId");
		InputStream inStream = null;
		if (faxId != null && !(faxId.equals(""))) {
			//獲取InputStream
			inStream = this.faxServerService.getFaxInAtt(faxId);
		}
		final InputStream in = inStream;
		final String contentType = "application/file";
		// 建<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瀏覽器在下載文件時
		// 沒法在保存文件對話框中顯示正確的文件名
		response.setHeader("content-disposition", "attachment; filename="
				+ faxId);
		return new DownloadAction.StreamInfo() // 使用隱式的方法實現了StreamInfo接口
		{
			public String getContentType() {
				return contentType;
			}
			public InputStream getInputStream() throws IOException {
				return in;
			}
		};
	}
	// 若是Struts動做不加file請求參數,則經過execute方法將指定目錄中文件列表輸出到客戶端
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 當file參數存在時,則調用DownloadAction中的execute方法
		// 實際上,在DownloadAction類中的execute方法調用了getStreamInfo方法
		// 這條語句就至關於調用了getStreamInfo方法
		return super.execute(mapping, form, request, response);
	}
}

二、第二種本身寫輸出流java

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