</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;
}
}