jsp下载的几种方式:1、运用struts的下载类DownloadAction 2、直接流化

1、第一种使用struts下载比较简单。需要继承DownloadAction。然后重写getStreamInfo方法。把InputStream流放入内部类中就行了。

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);
	}
}

2、第二种自己写输出流

/**
	 * 下载附件
	 * 
	 * @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;
	}

你可能感兴趣的:(jsp,exception,struts,String,null,byte)