现在的位置: 首页 > 综合 > 正文

Struts2直接下载从数据中的blob类型字段( Struts2.0下载文件流)

2013年07月01日 ⁄ 综合 ⁄ 共 2283字 ⁄ 字号 评论关闭
package com.ucit.ca.webApp.action;

import java.io.InputStream;
import java.sql.SQLException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.ucit.ca.webApp.po.User;
import com.ucit.ca.webApp.service.ICheckAppService;
import com.ucit.ca.webApp.service.IDownLoadsService;
import com.ucit.ca.webApp.service.Impl.DownLoadsServiceImpl;
/**
 * 下载文件
 * @author JiF
 *
 */
public class DownLoadsAction extends ActionSupport {
	private static final Log log = LogFactory.getLog(DownLoadsAction.class);
	@Resource
	private IDownLoadsService downLoadsService;	//下载服务层
	@Resource
	private ICheckAppService checkAppService;	//检查登陆用户对应用的权限
	
	
	private String kid;
	// Report输出流   
	public InputStream resultStream;   

	// 输出流Content Type   
	public String contentType;   
	
	// 输出流的生成的文件名   
	public String fileName;  
	

	/**
	 * 证书下载
	 * @return
	 */
	public String getP12(){
		HttpServletRequest request = ServletActionContext.getRequest();
		
		User user; 
		try {
			user=checkAppService.CheckLoginUser(request);
			if (user==null && !user.getUid().equals(kid)){ 
				log.error("log24");
				addActionMessage("log24");
				return INPUT;
			}
			resultStream=downLoadsService.getP12(kid);
		} catch (SQLException e) {		//获取P12文件异常
			log.error(e.getMessage());
			addActionError(e.getMessage());
			return INPUT;
		} catch (Exception e) {			//获取帐号有异常
			log.error(e.getMessage());
			addActionError("e.getMessage()");
			return INPUT;
		}
		contentType = "application/x-pkcs12"; 
		fileName = "attachment; filename=\""+user.getUloginId()+".p12\"";
		log.info("down p12 Success");
		return SUCCESS;
	}


	public String getKid() {
		return kid;
	}


	public InputStream getResultStream() {
		return resultStream;
	}


	public String getContentType() {
		return contentType;
	}


	public String getFileName() {
		return fileName;
	}


	public void setKid(String kid) {
		this.kid = kid;
	}

}

 

<!-- 下载-->
    <package name="down" namespace="/Down" extends="global_package">  
        <action name="downLoad_*" method="{1}" class="downLoadAction">  
                    <result name="success" type="stream">  
	                      	<!-- 对应contentType属性 -->  
				            <param name="contentType">${contentType}</param>  
				            <!--对应的InputStream的属性名 -->  
				            <param name="inputName">resultStream</param>  
				            <!-- 对应属性fileName,定义流输出格式 -->  
				            <param name="contentDisposition">${fileName}</param>  
				            <!-- 定义bufferSize,可选 -->  
				            <param name="bufferSize">1024</param>  
        			</result>  
        			<result name="input">/down/i_{1}.jsp</result>
        </action>  
    </package>

抱歉!评论已关闭.