下载文件示例

  闲来无事做了个文件下载的功能,这还是第一次做的说,不知道会不会遭BS。
请自备jQuery环境。
(function($){
	var _cf = window["configFile"] = {LoginModel:function () {}};
	
	_cf.LoginModel.prototype={
		/**
		* 下载配置文件
		*/
		downloadBk : function(fBKName,mosn){
			var action = window["path"]+"/bk_downloadBk.action";
			var $form = $('<form id="downloadBk" method="get" action="'+action+'"></form>');
			$form.append('<input type="hidden" name="fBKName" value="'+fBKName+'" />');
			$form.append('<input type="hidden" name="mosn" value="'+mosn+'" />');
			$form.append('<input type="hidden" name="random" value="'+Math.random()+'" />');
			$('body').append($form);
			$form[0].submit();
			$('#downloadBk').remove();
		}
	};
})(jQuery);

struts.xml
		<action name="bk_*" method="{1}"
			class="itims.fileBk.action.FileBkViewer">
			<interceptor-ref name="collectorStack"></interceptor-ref>
			<result name="download" type="stream">
				<param name="contentType">application/octet-stream</param>
				<param name="inputName">inputStream</param>
				<param name="contentDisposition">attachment;filename="${fileName}"</param>
				<param name="bufferSize">4096</param>
			</result>
		</action>


	public String downloadBk(){
		return "download";
	}
	public InputStream getInputStream(){
		LOG.info("start to download:"+fBKName);
		InputStream is=null;
		try {
			BKViewMgr bMgr = new BKViewMgr(dmsn);
			String fbkPath=bMgr.getCompareFilePath(fBKName,mosn);
			LOG.info("fbkPath:"+fbkPath);
			return new FileInputStream(fbkPath);
		} catch (FileNotFoundException e) {
			LOG.info("没有指定文件");
		}
		LOG.info("end to download:"+fBKName);
		return is;
	}
	public String getFileName(){
		String fileName=fBKName;
		try {
			return new String(fileName.getBytes(),"ISO8859-1");
		} catch (UnsupportedEncodingException e) {
			LOG.info("不支持的编码格式",e);
		}
		return "";
	}


你可能感兴趣的:(下载文件)