查看附件简记

</pre>总体思路:在jsp页面使用<iframe>来显示后台读到的流。<p></p><p>JSP:</p><p></p><pre name="code" class="html"><body>
  	<table width="100%" class="sou">
		<tr>  
			<td>
	   			<iframe  src="${basePath}/.../fileLoad.action?customerMessage.id=${customerMessage.id}" style="width: 100%; height: 700px;border: 0"></iframe>
			</td>
  		</tr>
  		<tr>
  			<td align="center">
  				<input type="button" value="返回" οnclick="javascript:history.back(-1);">
  			</td>
  		</tr>
  	</table>
  </body>

XML:

<action name="fileLoad" class="com.ccxe.p2p.action.FileUpAndDownLoadAction">
			<result name="success" type="stream">
				<!-- 指定下载文件的内容类型,text/plain是默认类型 -->
				<param name="contentType">${contentType}</param>
				<!--
					inputName默认值是inputStream,如果action中用于读取下载文件内容的属性名是inputStream,那么可以省略这个参数
				-->
				<param name="inputName">inputStream</param>
				<!--动态获取文件名,从Action中的取得filename-->
				<param name="contentDisposition">${loadFileName}</param>
				<param name="bufferSize">2048</param>
			</result>
		</action>

JAVA :

读取流及设置contentType

/**
 * Desc 文件操作类
 */
public class FileUpAndDownLoadAction extends ActionSupport {

    Logger log = Logger.getLogger(FileUpAndDownLoadAction.class);

    /**
     * 
     */
    private static final long serialVersionUID = 2451501278444274004L;

    private String loadFileName;
    private String contentType;
    private InputStream inputStream;
    private ICustomerMessage customerMessage;
    
    private IICustomerMessageService customerMessageService;
    
    public ICustomerMessage getCustomerMessage() {
		return customerMessage;
	}

	public void setCustomerMessage(ICustomerMessage customerMessage) {
		this.customerMessage = customerMessage;
	}

  
    @Override
    public String execute() throws Exception {
        String path = null;
        if(null != customerMessage){
			customerMessage = customerMessageService.fetchICustomerMessage(customerMessage);
        	path = customerMessage.getAffix();
        }
        if (path != null) {
            checkFormat(path);
            inputStream = new FileInputStream(getLoadFileName());
            return SUCCESS;
        } else {
            return ERROR;
        }
    }

    public InputStream getInputStream() throws Exception {
        inputStream = new FileInputStream(getLoadFileName());
        return inputStream;
    }


    public String fileAbsoluteToRelativeDir(String path) {
        return path;
    }

    /**  
     * 检查格式,文件后缀  
     * @param fileName  
     */
    private void checkFormat(String fileName) {

        String[] splitString = fileName.split("\\.");
        int length = splitString.length;
        String format = "";
        if (length > 1) {
            format = splitString[length - 1];
        }
        if (format.equalsIgnoreCase("xls")) {
            // contentType设定       
            contentType = "application/vnd.ms-excel;charset=utf-8";
            // attachment表示网页会出现保存、打开对话框       
//            loadFileName = "inline; filename=" + fileName;
            loadFileName = fileName;
        } else if (format.equalsIgnoreCase("xlsx")) {
            // contentType设定       
            contentType = "application/vnd.ms-excel;charset=utf-8";
            // attachment表示网页会出现保存、打开对话框       
//            loadFileName = "inline; filename=" + fileName;
            loadFileName = fileName;
        } else if (format.equalsIgnoreCase("pdf")) {
            // contentType设定       
            contentType = "application/pdf;charset=utf-8";
            // attachment表示网页会出现保存、打开对话框       
//            loadFileName = "inline; filename=" + fileName;
            loadFileName = fileName;
        } else if (format.equalsIgnoreCase("doc")) {
            // contentType设定       
            contentType = "application/msword;charset=utf-8";
            // attachment表示网页会出现保存、打开对话框       
//            loadFileName = "inline; filename=" + fileName;
            loadFileName = fileName;
        } else if (format.equalsIgnoreCase("docx")) {
            // contentType设定       
            contentType = "application/msword;charset=utf-8";
            // attachment表示网页会出现保存、打开对话框       
//            loadFileName = "inline; filename=" + fileName;
            loadFileName = fileName;
        } else if (format.equalsIgnoreCase("txt")) {
            // contentType设定       
            contentType = "text/plain;charset=utf-8";
            // attachment表示网页会出现保存、打开对话框       
//            loadFileName = "inline; filename=" + fileName;
            loadFileName = fileName;
        } else {
            // contentType设定       
            contentType = "text/plain;charset=utf-8";
            // attachment表示网页会出现保存、打开对话框       
//            loadFileName = "inline; filename=" + fileName;
            loadFileName = fileName;
        }
    }


    public String getLoadFileName() {
        return loadFileName;
    }

    public void setLoadFileName(String loadFileName) {
        this.loadFileName = loadFileName;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

	public void setCustomerMessageService(
			IICustomerMessageService customerMessageService) {
		this.customerMessageService = customerMessageService;
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值