/*
* 文件名:DownLoadAction.java
* 版权:Copyright 2016 Huawei Tech. Co. Ltd. All rights Reserved.
* 描述:
* 修改人:cWX340527
* 修改时间:2016年3月4日
* 修改内容:新增
*/
package com.huawei.srcms.securityreport.web;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.apache.struts2.ServletActionContext;
import sun.misc.BASE64Encoder;
import com.huawei.srcms.common.base.web.BaseAction;
import com.huawei.srcms.common.exception.SRCMSException;
import com.huawei.srcms.common.util.SRCMSUtils;
import com.huawei.srcms.securityreport.service.ISecurityReportService;
/**
* 下载文件Action<描述>.
* @author cWX340527
* @version 1.0, 2016年3月4日
*/
public class DownLoadAction extends BaseAction
{
private static final long serialVersionUID = 1L;
private ISecurityReportService securityReportService;
/**
* 文件流
*/
private InputStream inputStream;
/**
* 需要下载的文件名称
*/
private String filename;//下载页面传递了该参数,需提供setter方法接收
public void setFilename(String filename)
{
//转码并防御xss攻击转换
try
{
this.filename = SRCMSUtils.xssfilter(new String(filename.getBytes("ISO-8859-1"), "utf-8").trim());
}
catch (UnsupportedEncodingException e)
{
LOG.error("setFilename is fail:"+e.toString());
}
}
public String downloadFile() throws SRCMSException
{
try
{
this.inputStream = this.securityReportService.downloadfile(this.filename);
}
catch (FileNotFoundException e)
{
LOG.error("get " + this.filename + " exception.", e.getMessage());
throw new SRCMSException("get " + this.filename + " exception.");
}
//返回一个文件输入流
if (this.inputStream == null)
{
this.message = "下载文件【" + this.filename + "】失败。";
return ERROR;
}
return SUCCESS;
}
//为客户端提供输入流
public InputStream getInputStream()
{
return this.inputStream;
}
//根据不同的文件动态给出MIME文件类型
public String getContentType()
{
//在Tomcat Conf里的web.xml有对应的映射文件
return ServletActionContext.getServletContext().getMimeType(this.filename);
}
//返回一个文件名
public String getFilename() throws IOException
{
String agent = ServletActionContext.getRequest().getHeader("user-agent");//根据http头信息获取对应的浏览器类型
return encodeDownloadFilename(filename, agent);
}
//下载附件名乱码问题 , IE和火狐 解决不同 IE默认是Url编码 火狐默认是base64编码
public String encodeDownloadFilename(String fileName, String agent) throws IOException
{
if (agent.contains("Firefox"))
{ // 火狐浏览器
fileName = "=?UTF-8?B?" + new BASE64Encoder().encode(fileName.getBytes("utf-8")) + "?=";
}
else
{ // IE及其他浏览器
fileName = URLEncoder.encode(fileName, "utf-8");
}
return fileName;
}
/**
* @return the service
*/
public ISecurityReportService getSecurityReportService()
{
return this.securityReportService;
}
/**
* @param service the service to set
*/
public void setSecurityReportService(ISecurityReportService service)
{
this.securityReportService = service;
}
}
* 文件名:DownLoadAction.java
* 版权:Copyright 2016 Huawei Tech. Co. Ltd. All rights Reserved.
* 描述:
* 修改人:cWX340527
* 修改时间:2016年3月4日
* 修改内容:新增
*/
package com.huawei.srcms.securityreport.web;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.apache.struts2.ServletActionContext;
import sun.misc.BASE64Encoder;
import com.huawei.srcms.common.base.web.BaseAction;
import com.huawei.srcms.common.exception.SRCMSException;
import com.huawei.srcms.common.util.SRCMSUtils;
import com.huawei.srcms.securityreport.service.ISecurityReportService;
/**
* 下载文件Action<描述>.
* @author cWX340527
* @version 1.0, 2016年3月4日
*/
public class DownLoadAction extends BaseAction
{
private static final long serialVersionUID = 1L;
private ISecurityReportService securityReportService;
/**
* 文件流
*/
private InputStream inputStream;
/**
* 需要下载的文件名称
*/
private String filename;//下载页面传递了该参数,需提供setter方法接收
public void setFilename(String filename)
{
//转码并防御xss攻击转换
try
{
this.filename = SRCMSUtils.xssfilter(new String(filename.getBytes("ISO-8859-1"), "utf-8").trim());
}
catch (UnsupportedEncodingException e)
{
LOG.error("setFilename is fail:"+e.toString());
}
}
public String downloadFile() throws SRCMSException
{
try
{
this.inputStream = this.securityReportService.downloadfile(this.filename);
}
catch (FileNotFoundException e)
{
LOG.error("get " + this.filename + " exception.", e.getMessage());
throw new SRCMSException("get " + this.filename + " exception.");
}
//返回一个文件输入流
if (this.inputStream == null)
{
this.message = "下载文件【" + this.filename + "】失败。";
return ERROR;
}
return SUCCESS;
}
//为客户端提供输入流
public InputStream getInputStream()
{
return this.inputStream;
}
//根据不同的文件动态给出MIME文件类型
public String getContentType()
{
//在Tomcat Conf里的web.xml有对应的映射文件
return ServletActionContext.getServletContext().getMimeType(this.filename);
}
//返回一个文件名
public String getFilename() throws IOException
{
String agent = ServletActionContext.getRequest().getHeader("user-agent");//根据http头信息获取对应的浏览器类型
return encodeDownloadFilename(filename, agent);
}
//下载附件名乱码问题 , IE和火狐 解决不同 IE默认是Url编码 火狐默认是base64编码
public String encodeDownloadFilename(String fileName, String agent) throws IOException
{
if (agent.contains("Firefox"))
{ // 火狐浏览器
fileName = "=?UTF-8?B?" + new BASE64Encoder().encode(fileName.getBytes("utf-8")) + "?=";
}
else
{ // IE及其他浏览器
fileName = URLEncoder.encode(fileName, "utf-8");
}
return fileName;
}
/**
* @return the service
*/
public ISecurityReportService getSecurityReportService()
{
return this.securityReportService;
}
/**
* @param service the service to set
*/
public void setSecurityReportService(ISecurityReportService service)
{
this.securityReportService = service;
}
}