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浏览器

猜你喜欢

转载自fuanyu.iteye.com/blog/1064490
今日推荐