java 实现下载文件 支持safari,ff,ie等

 点击执行的action

	public String docattach (){
		String jssessionid=CookieMgr.readCookies(this.getRequest(),"jsessionid");
		String strAttchId = this.getRequest().getParameter("attachid");
		HashMap map = new HashMap();
		InputStream inputStream=null;
		try {
			inputStream = docuInfoManager.getattachment(Long.parseLong(strAttchId),map);
			if (inputStream != null) {
				inputStreamMap.put(strAttchId+jssessionid,inputStream);
				DocAttachmentInfo docattachmentInfo = (DocAttachmentInfo) map
				.get("docattachmentInfo");

				if (docattachmentInfo.getAttchMimetype().equalsIgnoreCase(""))
					if (docattachmentInfo.getAttchFileext()
							.equalsIgnoreCase(".jpg"))
						setContentType("image/jpg");
					else if (".pdf".equals(docattachmentInfo.getAttchFileext()))
							setContentType("application/pdf");
					else if (".doc".equals(docattachmentInfo.getAttchFileext()))
						    setContentType("application/msword");
					else if (".xls".equals(docattachmentInfo.getAttchFileext()))
						    setContentType("application/vnd.ms-excel");
					else if (".rar".equals(docattachmentInfo.getAttchFileext()))
					    setContentType("application/x-zip-compressed");
				else
					setContentType(docattachmentInfo.getAttchMimetype());// 不同类型的文件对应不同的MIME类型
				// 文件名
				String fName = docattachmentInfo.getAttchFilename();
				fName = new String(fName.getBytes("UTF-8"),"iso-8859-1"); 
				setDownloadFileName(fName); 
				return "docattach";
			} else {
				this.getRequest().setAttribute("message", "附件不存在!");
				return this.ERROR;
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.getRequest().setAttribute("message", "系统出现异常!");
			return this.ERROR;
		} 
	}
	

 点击下载保存后的流   

	public InputStream getInputStreamAttachment() {
		InputStream inputStream=null;
		 String jssessionid=CookieMgr.readCookies(this.getRequest(),"jsessionid");
		 if(null!=jssessionid){
			String strAttchId = this.getRequest().getParameter("attachid");
			if(inputStreamMap.get("attchId")!=null) {
				strAttchId = inputStreamMap.get("attchId").toString();
				inputStreamMap.remove("attchId");
			}
			inputStream = (InputStream) inputStreamMap.get(strAttchId  + jssessionid);
			inputStreamMap.remove(strAttchId+ jssessionid ); 
		}
		return inputStream;
	}

 

struts.xml配置文件

<action name="emobileservices" class="emobileSearchAction" >
			<result name="ajaxpage">/apps/fulltextsearch/search/emobile/EmobileSearch.jsp</result>
			<result name="success">/apps/fulltextsearch/search/emobile/EmobileSearchIndex.jsp</result>
			<result name="docattach" type="stream">
			    <param name="contentType">${contentType}</param>
                <param name="inputName">inputStreamAttachment</param> 
                <param name="bufferSize">4096</param>
             	<param name="contentDisposition">attachment;filename="${downloadFileName}"</param>
            </result> 
            <result name="docDetail">/apps/fulltextsearch/search/emobile/emobileDocDetail.jsp</result>
			<result name="error">/common/emobileErrMsg.jsp</result>
		</action> 

 

注意:此配置将会支持safari浏览器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值